首页 文章

社交网站 - 成功登录后无法登录Laravel应用程序

提问于
浏览
1

我正在尝试使用Laravel和socialite设置Google登录 . 我已将所有内容设置为进入Google登录页面并将我重定向回应用程序 . 但是,当我被重定向时,我总是在“登录”页面而不是“主页”页面上 . 成功登录Google后,我没有登录我的Laravel应用程序 . 怎么了?

/**
 * Obtain the user information from Google.
 *
 * @return \Illuminate\Http\Response
 */
public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('google')->user();
    } catch (\Exception $e) {
        return redirect('/login');
    }

    // check if they're an existing user
    $existingUser = User::where('email', $user->email)->first();

    if($existingUser){
        // log them in
        auth()->login($existingUser, true);
    } 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()->to('/home');

}

尽管要求登录用户,但在重定向回应用程序时我没有登录 . 如何确保登录Google后我已登录Laravel应用程序?

Update

事实证明当$ user变量从Google返回时我得到了 InvalidStateException

enter image description here

try catch块实际上会抛出异常 . 如果我发现异常它看起来像:

enter image description here

事实证明,在我的 .env 文件中有什么打破了它:

SESSION_DOMAIN=http://localhost:8000

删除此行修复了问题!

1 回答

  • 0

    .env 文件中删除 SESSION_DOMAIN

相关问题