首页 > 解决方案 > 成功认证后 Laravel 社交名流登录不重定向

问题描述

我有一个使用 google 登录的 laravel 项目。我正在使用来自 laravel 的社交名流包。在他们在谷歌身份验证屏幕中确认后,我可以将用户的信息保存在我的数据库中,但之后它将始终重定向到登录。似乎Auth::login($user)不起作用。我错过了什么?

这是我的登录控制器

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


public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect('/login');
    }

    $existingUser = User::where('email', $user->email)->first();
    if($existingUser){
        //login the user
        \Auth::login($existingUser,true);
        return redirect('/home');
    } else {
        // create a new user
        $newUser                  = new User;
        $newUser->name            = $user->name;
        $newUser->email           = $user->email;
        $newUser->google_id       = $user->id;
        $newUser->avatar          = $user->avatar;
        $newUser->avatar_original = $user->avatar_original;
        $newUser->save();

        \Auth::login($newUser,true);
    }
    return redirect('/home');
}

路线

Route::get('/redirect', 'Auth\LoginController@redirectToProvider');
Route::get('/callback', 'Auth\LoginController@handleProviderCallback');

来自谷歌控制台的授权重定向 URI

http://localhost:8000/callback
http://localhost:8000/home

使用社交名流在 laravel 中使用 google 登录参考:链接在这里

当我检查网络时。/home路径写为响应 302 。在此处输入图像描述

标签: phplaravelauthenticationgoogle-apilaravel-socialite

解决方案


我只是通过domain在我的 session.php 中设置 null 来解决它。我不知道这是否是正确的答案,但我现在可以在使用谷歌登录后重定向到我的主页。也许如果我将它部署到生产中,我会将其更改domain为我的服务器的实际 url。

/*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => null,

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you if it can not be done securely.
    |
    */

    'secure' => false,

推荐阅读