首页 文章

无法在Laravel 5.1中通过Facebook登录

提问于
浏览
0

我正在尝试使用Laravel 5.1登录facebook .

我在laravel文档中提到了每个步骤 . http://laravel.com/docs/5.1/authentication#social-authentication .

但是,当我通过Facebook登录时,它将重定向到我的正常登录页面 . 在排序会话中存储在facebook登录中 .

这是我编写的代码 .

Router.php

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

AuthController.php

public function redirectToProvider()
{
    return  Socialite::driver('facebook')
            ->scopes(['email', 'public_profile'])
            ->redirect();
}

public function handleProviderCallback()
{
    $user = Socialite::driver('github')->user();
    $user = Socialite::driver('github')->user();
    // OAuth Two Providers
    $token = $user->token;
    // OAuth One Providers
    $token = $user->token;
    $tokenSecret = $user->tokenSecret;
    // All Providers
    $user->getId();
    $user->getNickname();
    $user->getName();
    $user->getEmail();
    $user->getAvatar();
}

Services.php

'facebook' => [
    'client_id' => '1625567400000000',
    'client_secret' => 'secret',
    'redirect' => 'http://localhost:8000/',
 ],

当我输入localhost / 8000 / auth / facebook时,它会将我重定向到facebook并询问public_profile,email等的权限 . 它将重定向回localhost / auth / login .

当我在URL中键入localhost:8000 / auth / facebook / callback时,它将通过这样的错误;

Middleware.php第69行中的ClientException:

客户端错误:404

2 回答

  • 1

    对于您的情况,我邀请您使用中间件来检查用户是否已经登录 . 这可能是您重定向到的问题 localhost/auth/login

    我希望以下代码对您有用

    public function handleProviderCallback()
    {        
        //retrieve user's information from facebook
        $socUser  = Socialite::driver('facebook')->user();
    
        //check user already exists in db
        $user = \App\User::where('email', $socUser->getEmail())->first();
        if($user) {
            // if exist, log user into your application
            //  and redirect to any path you want
            \Auth::login($user);
            return redirect()->route('user.index');
        }
    
        //if not exist, create new user, 
        // log user into your application 
        // and resirect to any path you want
        $user = new \App\User ;
        $user->email = $socUser->getEmail();
        // ...
        // ...
        // ...
        $user->save();
        \Auth::login($user); // login user
        return redirect()->route('user.index'); // redirect
    }
    

    注意:我没有测试我的代码,但你应该知道

    欲了解更多信息:http://laravel.com/docs/5.1/authentication

    并且正如@mimo提到的那样,

    您的Services.php文件中的重定向URL必须是localhost:8000 / auth / facebook / callback

  • 0

    您的Services.php文件中的重定向URL必须是

    localhost:8000/auth/facebook/callback
    

相关问题