Search code examples
laravellaravel-5laravel-socialite

login through specific guard using socialite in laravel


I have this function for google login

function :

  public function loginWithGoogle()
    { 
        try {
            $googleUser = Socialite::driver('google')->user();
            $user = User::where('google_id', $googleUser->id)->first();

            if($user){
                Auth::login($user);
                return redirect('/home');
            }

            else{
                $createUser = User::create([
                    'name' => $googleUser->name,
                    'email' => $googleUser->email,
                    'google_id' => $googleUser->id,
                    'password' => encrypt('test@123')
                ]);

                Auth::login($createUser);
                return redirect('/home');
            }

        } catch (Exception $exception) {
            dd($exception);
        }
    }

but i have created a specific guard for user login :

   'user' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

how i will change the guard to user when i login in the application through socialite Any suggestion Thanks.


Solution

  • If needed, you may specify an authentication guard before calling the login method:

    Auth::guard('user')->login($user);
    

    Docs: https://laravel.com/docs/8.x/authentication#authenticate-a-user-instance