Laravel 4 Remember me expire time
Asked Answered
H

1

13

I am fairly new to Laravel and had a question regarding the remember me function.

I have successfully enabled the "remember me" function by attaching a second argument to the Auth::attempt method like so.

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
    // The user is being remembered...
}

As noted in the documentation, this enables remember me indefinitely or until an user manually logs out.

I essentially want to set an expire date on the "remember me" function.

Looking at the console, I can see that enabling "remember me" generates a remember_{HASH} cookie.

What would be the best way to overwrite the expire date specified in this cookie to let say a week in the future? The cookie currently sets the date in the past, which makes it last forever.

Keep in mind that I had to set 'lifetime' => 0 in sessions.php so that I can trigger the remember me function based on user preference so changing this into a week would not work in my case.

Thanks!

Howlan answered 6/8, 2013 at 1:26 Comment(2)
Laravel sets the expiration time to +5years. No, you can't unless you change the core, because Auth::attempt() uses Cookie::forever() method to store the cookie and the expiration time is hard Coded into it.. So to set the expiration time you have to lay hands on the Laravel core.Criticism
I believe you could create your own package and use that in place of the 'Auth' package bundled with Laravel. However if you are new to laravel then it might not be suitable.Thaddeusthaddus
C
16

I don't know whether its a good way to do it or not, but if you are desperate to set the expiration time for remember me you can try this, it works for me though.

Let the laravel's Auth::atempt($credential,true) do its normal business, i.e, setting the remember me expiration time to 5 years

We will change this in App::after() method, so in your filters.php file find App::after() method and change the cookie expiration time

App::after(function($request, $response)
{
  if ( Auth::check()){
      $ckname=Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
      $ckval=Cookie::get($ckname); //Get the value of the cookie
      return $response->withCookie(Cookie::make($ckname,$ckval,360)); //change the expiration time
  }
});

Note: Cookie::make('name','value','expiration time');

Criticism answered 6/8, 2013 at 16:2 Comment(1)
Thanks for the answer! This works great and I'd rather do this than edit the core files.Howlan

© 2022 - 2024 — McMap. All rights reserved.