duandanbeng1829 2015-09-13 13:24
浏览 52
已采纳

Facebook社交名媛登录Laravel 5.1

I have followed all the step in http://laravel.com/docs/5.1/authentication to make the social login with facebook on Laravel 5.1.

Here the steps that I've followed:

1 - Use the command in my Laravel root project:

composer require laravel/socialite

2 - register the Laravel\Socialite\SocialiteServiceProvider in your config/app.php configuration file:

'providers' => [
    // Other service providers...

    Laravel\Socialite\SocialiteServiceProvider::class,
],

3 - Add the Socialite facade to the aliases array in your app configuration file:

'Socialite' => Laravel\Socialite\Facades\Socialite::class,

4 - add the facebook services in config/services.php:

 'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => 'http://homestead.app/auth/facebook/callback',
    ],

5 - add to my AuthController.php the methods redirectToProvider() and handleProviderCallback():

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Redirect path after successful login
     *
     * @var string
     */
    protected $redirectPath = '/greeting';

    /**
     * Redirect path after failed login
     *
     * @var string
     */
    protected $loginPath = '/auth/login';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     * Redirect the user to the Facebook authentication page.
     *
     * @return Response
     */


    public function redirectToProvider()
    {
        return Socialite::driver('facebook')->redirect();
    }

    /**
     * Obtain the user information from Facebook.
     *
     * @return Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('facebook')->user();

        dd($user);
    }
}

I want to highlight that I have created AuthController.php in the folder app/Http/Controllers/Auth/ because after it asks to create the routes: 'Auth\AuthController@redirectToProvider' 'Auth\AuthController@handleProviderCallback'

6 - I've added the routes:

Route::get('auth/facebook', 'Auth\AuthController@redirectToProvider');
Route::get('auth/facebook/callback', 'Auth\AuthController@handleProviderCallback');

Now, when I click "Login with facebook" in my login page it goes to http://homestead.app/auth/facebook and get me the following error:

FatalErrorException in AuthController.php line 90: Class 'Socialite' not found

I have tried to follow the codeanchor guide but it is for laravel 5.0 (I need to use that on laravel 5.1)

  • 写回答

1条回答 默认 最新

  • douou2026 2015-10-31 15:30
    关注

    Finally I have solved with deleting everything and installing Homestead again from the start

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的