Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify "remember me" expired time in Laravel 5.2+?

Tags:

laravel

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?

like image 759
Hexor Avatar asked Oct 13 '25 09:10

Hexor


2 Answers

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);
like image 123
3 revs, 3 users 75%高璐璐 Avatar answered Oct 15 '25 08:10

3 revs, 3 users 75%高璐璐


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

like image 33
Mushlih Almubarak Avatar answered Oct 15 '25 06:10

Mushlih Almubarak