繁体   English   中英

Laravel 身份验证成功后社交名流登录未重定向

[英]Laravel socialite login after successful auth not redirecting

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

这是我的登录名 controller

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 使用社交名流与谷歌登录: 链接在这里

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

我刚刚通过在我的 session.php 中创建domain 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,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM