代码之家  ›  专栏  ›  技术社区  ›  Connor Leech

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

  •  1
  • Connor Leech  · 技术社区  · 6 年前
    { } //登录 }其他{ $new user=新用户; $newuser->email=$user->电子邮件; $newuser->avatar=$user->avatar; $newuser->保存(); } return redirect()->到(“/home”); }

    尽管调用了登录用户,但当我重定向回应用程序时,我没有登录。我怎样才能确保在使用谷歌登录后,我已经登录到Laravel应用程序?

    更新

    当$user变量从google返回时,我得到一个invalidStateException

    Try-Catch块实际上引发了一个异常。如果我添加异常,它看起来像:

    结果发现,我的.env>文件中包含了破坏它的内容:

    会话\域=http://localhost:8000

    删除此行修复了问题!

    /**
     * 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');
    
    }
    

    尽管调用了登录用户,但当我重定向回应用程序时,我没有登录。我怎样才能确保在使用谷歌登录后,我已经登录到Laravel应用程序?

    更新

    当$user变量从google返回时,我得到一个InvalidStateException

    enter image description here

    Try-Catch块实际上引发了一个异常。如果我添加异常,它看起来像:

    enter image description here

    .环境

    SESSION_DOMAIN=http://localhost:8000

    删除此行修复了问题!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Connor Leech    6 年前

    SESSION_DOMAIN