Laravel Socialite doesn't work on Google Chrome

Viewed 866

I have one problem with Laravel Socialite login, in my Chrome works normally but in rest of the people browser doesn't work (works in other browsers). Before php update in server to 7.3.18 from 7.1 and update to Laravel 6 from 5.8, all works normally. I try to clear all caches, change session mode to cookie(file before), clear session and cookies in browser but nothing solved the problem.

When try to login, give me this

And this is my code:

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());
}
1 Answers

I understand what you are trying to do but sometimes less is more and more is less..... the call back is being made by the provider and not the user. anyway have different methods for each social login

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

With your methods separated you will have different routes for different social login which IMO is far better as they are have slightly different return params and you may want to perform additional function for a particular social login in future.

Related