[英]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. 我正在使用Socialite进行用户登录,并且我想设置一个
remember_token
以在用户通过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? 问题是,当用户回来时,他们不会被记住并自动登录。如何使用Socialite做到这一点?
According to the documentation , passing true
as the second argument of login()
will set the remember token. 根据文档 ,传递
true
作为login()
的第二个参数将设置记住标记。
// Login and "remember" the given user... Auth::login($user, true);
The Auth
facade and auth()
helper function access the same object. Auth
外观和auth()
帮助函数访问同一对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.