简体   繁体   中英

Laravel Socialite Remember Me

I am using Socialite for user logins and I would like to set a remember_token to remember the user when they login through Socialite.

Right now I have the following service to create or log the user in:

class SocialAccountService {

    public function createOrGetUser(ProviderUser $providerUser) {

        $account = SocialAccount::whereProvider('google')
            ->whereProviderUserId($providerUser->getId())
            ->first();

        if ($account) {
            return $account->user;
        } else {
            $account = new SocialAccount([
                'provider_user_id' => $providerUser->getId(),
                'provider' => 'google'
            ]);

            $user = User::whereEmail($providerUser->getEmail())->first();
            if (!$user) {
                $user = User::create([
                    'email' => $providerUser->getEmail(),
                    'name' => $providerUser->getName()
                ]);
            }
            $account->user()->associate($user);
            $account->save();
            return $user;
        }
    }
}

It is called with the following controller:

class AuthController extends Controller {

    public function logout() {
        Auth::logout();
        return redirect('/');
    }

    public function redirectToGoogle() {
        return Socialite::driver('google')->redirect();
    }

    public function handleGoogleCallback(SocialAccountService $service) {
        $user = $service->createOrGetUser(Socialite::driver('google')->user());
        auth()->login($user);
        return redirect('/');
    }
}

The issue is that when the user comes back they are not remembered and automatically logged in. How can I do this with Socialite?

According to the documentation , passing true as the second argument of login() will set the remember token.

// Login and "remember" the given user... Auth::login($user, true);

The Auth facade and auth() helper function access the same object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM