社交名流 - 成功登录后无法登录 Laravel 应用

Socialite - after successful login unable to log into Laravel app

我正在尝试使用 Laravel 和社交名流设置 Google 登录。我已设置好所有内容以转到 Google 登录页面并将我重定向回应用程序。但是,当我被重定向时,我总是会到达 "login" 页面而不是 "home" 页面。使用 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 应用程序?

更新

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

try catch 块实际上抛出异常。如果我添加异常,它看起来像:

事实证明,我的 .env 文件中的问题对我来说是错误的:

SESSION_DOMAIN=http://localhost:8000

删除此行解决了问题!

从您的 .env 文件中删除 SESSION_DOMAIN