Laravel turn off remember me when logging in

Viewed 2361

I am using the laravel/ui package as you can see here: https://laravel.com/docs/7.x/authentication#included-routing

I want to make the user choose whether he wants to be remembered or not when he is logging in. However, the default setting seems to be that every user is remembered. According to the documentation the LoginController is shipping with remember me functionality: https://laravel.com/docs/7.x/authentication#remembering-users

My question is how to change or override the LoginController so that the user can decide via checkbox whether he should be remembered or not.

Thanks in advance! Hope you can help me, I am quite new to Laravel

2 Answers

Remember user is not a default.

Documentation says:

If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the attempt method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your users table must include the string remember_token column, which will be used to store the "remember me" token.

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

When your user is logging in just send the value of your 'remember me' checkbox and use it in your attempt method.

if (Auth::attempt(['email' => $email, 'password' => $password], USE VALUE FROM  CHECKBOX HERE)) {
    // The user is being remembered...
}

Thanks for your help, I just found out how to solve the problem. ColinMD's comment is right, the Laravel UI package takes care of it. However, if you change the login View you and add your own remember me checkbox, you have to take care that this checkbox has name="remember" because the LoginController uses a trait that checks for the input "remember" and not "remember_me" or something other.

Furthermore I was wrong about the remember me functionality: Remember me means that if the session times out you still remain logged in. A laravel session times out after 2 hours. To quickly test whether or not your remember me checkbox works you can change the .env file settings to SESSION_LIFETIME=1 instead of SESSION_LIFETIME=120 and login to your page with the remember me checkbox checked. Wait some time longer then one minute and than close the browser window. If you then visit your website again with the same browser and are logged in (even though the session timed out) you have been remembered.

Remember me does NOT mean, that you are remembered when you close the browser window and visit the site again and are logged in automatically during the session lifetime. Remember me is only when the session lifetime is over!

I know that I am answering my own question, hope it helps someone who encountered the same problem. If I still got something wrong, please let me know.

Related