After using php artisan make:auth
, Laravel's "remember me" will remember the user indefinitely.
How to change that time? Like make it expire in 7 days?
Use @Hexor has a problem, when user first login, you can't use Cookie::get($rememberTokenName); it's empty!
You should get cookie queue value first, then reset cookie expire time.
$rememberTokenExpireMinutes = 20;
// 首先获取 记住我 这个 Cookie 的名字, 这个名字一般是随机生成的,
// First, get remember me cookie name. This is randomly generated.
$rememberTokenName = \Auth::getRecallerName();
$cookieJar = $this->guard()->getCookieJar();
$cookieValue = $cookieJar->queued($rememberTokenName)->getValue();
$cookieJar->queue($rememberTokenName, $cookieValue, $rememberTokenExpireMinutes);
$jumpUrl = '/user/xxxx';
return $this->authenticated($request, $this->guard()->user())
?: redirect()->intended($jumpUrl);
You can set the remember me cookie duration by adding 'remember' => 43800 //(use minutes)
in the config in config/auth.php
Just change this:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
to:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
'remember' => 43800 // Set remember me duration here
],
],
Note: The 'remember' key is a mandatory keyword because it will be read by laravel in Illuminate\Auth\AuthManager
namespace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With