首页 > 解决方案 > Laravel Socialite 不能在谷歌浏览器上运行

问题描述

我的 Laravel Socialite 登录有一个问题,在我的 Chrome 中正常工作,但在其他人的浏览器中不起作用(在其他浏览器中工作)。在服务器中的 php 从 7.1 更新到 7.3.18 并从 5.8 更新到 Laravel 6 之前,一切正常。我尝试清除所有缓存,将会话模式更改为 cookie(之前的文件),清除浏览器中的会话和 cookie,但没有解决问题。

尝试登录时,给我这个

这是我的代码:

public function loginSocial(Request $request){
    $this->validate($request, [
        'social_type' => 'required|in:google,facebook'
    ]);
    $socialType = $request->get('social_type');
    return Socialite::driver($socialType)->stateless()->redirect();
}

public function loginCallback(Request $request){
    $socialType = $request->session()->get('social_type');
    //Aparently, this get give to $socialType null in ppl browser. I dont understand why this get doesn't works.
    $userSocial = Socialite::driver($socialType)->stateless()->user();
    //If use 'google' instead $socialType, works fine.
    $user = User::where('email',$userSocial->email)->first();
    \Auth::login($user);
    return redirect()->intended($this->redirectPath());
}

标签: phplaravelgoogle-chromelaravel-6laravel-socialite

解决方案


我了解您正在尝试做的事情,但有时越来越少……回电是由提供商而不是用户进行的。无论如何,每个社交登录都有不同的方法

// Google login
public function googleSocialLogin(Request $request){
    Socialite::driver('google')->stateless()->redirect();
}

// Google callback
public function googleSocialLoginCallback(){

    $userSocial = Socialite::driver('google')->stateless()->user();
    $user = User::where('email',$userSocial->email)->first();

    \Auth::login($user);
    return redirect()->intended($this->redirectPath());
}

// Facebook login
public function facebookSocialLogin(Request $request){
    Socialite::driver('facebook')->stateless()->redirect();
}

// Facebook callback
public function facebookSocialLoginCallback(){

    $userSocial = Socialite::driver('facebook')->stateless()->user();
    $user = User::where('email',$userSocial->email)->first();

    \Auth::login($user);
    return redirect()->intended($this->redirectPath());
}

将您的方法分开后,您将有不同的路线用于不同的社交登录,IMO 会更好,因为它们的返回参数略有不同,您可能希望将来为特定的社交登录执行附加功能。


推荐阅读