Laravel Socialite在Google Chrome上不起作用

问题描述 投票:1回答:1

我在Laravel Socialite登录时遇到一个问题,在我的Chrome浏览器中可以正常工作,但在其他人浏览器中则无法使用(在其他浏览器中也可以使用)。在将PHP从7.1服务器更新到7.3.18并从5.8更新到Laravel 6之前,所有工作正常。我尝试清除所有缓存,将会话模式更改为cookie(之前为文件),在浏览器中清除会话和cookie,但没有任何方法可以解决问题。

When try to login, give me this

这是我的代码:

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());
}
php laravel google-chrome laravel-6 laravel-socialite
1个回答
0
投票

我了解您要尝试做的事情,但有时少即是多越来越少.....回调是由提供者而不是用户进行的。无论如何,每个社交登录都有不同的方法

// 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会好得多,因为IMO的返回参数略有不同,并且您将来可能希望针对特定的社交登录执行其他功能。

© www.soinside.com 2019 - 2024. All rights reserved.