使用Laravel 5.1实现Facebook Socialite登录

6
我会尽力帮助您翻译。以下是需要翻译的内容:

我已经按照http://laravel.com/docs/5.1/authentication中的所有步骤,在Laravel 5.1中使用Facebook进行社交登录。

我已按照以下步骤进行:

1- 在我的Laravel根项目中使用命令:

composer require laravel/socialite

2 - 在您的config/app.php配置文件中注册Laravel\Socialite\SocialiteServiceProvider:

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

    Laravel\Socialite\SocialiteServiceProvider::class,
],

3-在您的应用程序配置文件中将Socialite门面添加到别名数组中:

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

4 - 在config/services.php中添加Facebook服务:

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

5 - 在我的AuthController.php中添加redirectToProvider()和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);
    }
}

我想强调的是,我已经在文件夹app/Http/Controllers/Auth/中创建了AuthController.php,因为在它要求我们创建路由之后,需要添加以下路由:'Auth\AuthController@redirectToProvider'和'Auth\AuthController@handleProviderCallback'。

6 - 我已经添加了路由:

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

现在,当我在登录页面点击“使用Facebook登录”时,它会跳转到http://homestead.app/auth/facebook,并显示以下错误信息:

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

我尝试按照指南操作,但该指南适用于 Laravel 5.0(而我需要在 Laravel 5.1 上使用)。

请在您的Composer中尝试使用"composer dump-autoload"命令。 - aldrin27
我刚刚尝试了,但是什么也没有改变。 - Giuseppe87
最终我解决了问题,通过删除所有内容并从头重新安装来解决。 - Giuseppe87
1
@Giuseppe兄弟,在下面回答你的问题并标记为解决方案,以防有人来到这个页面寻找答案 :) - Mohamed Mo Kawsara
1
晚了点,但如果你将来遇到这个问题,请尝试手动删除 bootstrap/cache/services.json 文件,然后运行 composer dump-autoload 命令。我曾经遇到过同样的问题,直到有人指出我的 homestead 环境中该文件存在问题。 - Prynz
显示剩余10条评论
1个回答

1

最终,我通过删除一切并重新从头安装Homestead来解决了问题。


@mkmnstr 那就尝试删除 vendor 文件夹中的所有内容,然后再次运行 composer install - totymedli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接