Table of contents

Laravel Login Remember Me

In this post, I will show you how to add or implement a remember me function to your Laravel login or authentication. One of the important implementations is to add your login with remember me option to your login because sometimes the user doesn't like to log in every time they access their account and automatically logged their account on their trusted browser.

 

laravel login with remember me

 

In my previous post, I posted on how to log in and register with email and username now also logout functionality. Now let's cater the remember me option.

 

I will give you easy steps for you to understand easily.

 

Step 1: Create Laravel App

I assume that you have already set up your composer on your system. Run the following coding to install the new Laravel app. However, you can skip this step if you have the Laravel app installed already.

composer create-project --prefer-dist laravel/laravel login-with-remember-me

 

Next, navigate the login-with-remember-me folder with the following command.

cd login-with-remember-me

 

Step 2: Setup Database Credentials

Next, create your database you can use the command to create a database or in PHPMyAdmin. Then once created navigate the .env file and update your database credentials.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password

 

For more information about the whole process of setting up your migrations and model for users please visit my previous post about authentication.

 

NOTE: Important to double-check we must verify if remember_token exists in the user's table fields. If not you must add remember_token to your user's table or follow my previous tutorial because Laravel already provided the default in migrations called $table->rememberToken();.

 

First, in your login blade template, we will add remember me input.

<div class="form-group mb-3">
      <label for="remember">Remember me</label>
      <input type="checkbox" name="remember" value="1">
</div>

 

As you can see below when submitting the login form and checking the remember me. It will show this submitted request when we print_r it.

laravel remember me login

Now let's change our previous code in my previous tutorial. In your App\Http\Controllers\LoginController.php we will change our login method and implement the remember me.

 

FROM THIS:

public function login(LoginRequest $request)
{
   $credentials = $request->getCredentials();

   if(!Auth::validate($credentials)):
      return redirect()->to('login')
                ->withErrors(trans('auth.failed'));
   endif;

   $user = Auth::getProvider()->retrieveByCredentials($credentials);

   Auth::login($user);

   return $this->authenticated($request, $user);
}

 

TO THIS:

public function login(LoginRequest $request)
{
   $credentials = $request->getCredentials();

   if(!Auth::validate($credentials)):
      return redirect()->to('login')
                ->withErrors(trans('auth.failed'));
   endif;

   $user = Auth::getProvider()->retrieveByCredentials($credentials);

   Auth::login($user, $request->get('remember'));

   return $this->authenticated($request, $user);
}

 

As you can see above I added $request->get('remember') to the second parameter of Auth::login() function.

 

Now let's try if working.

laravel remember me auth

 

Now when we Inspect our browser in Cookies we already see the remember_web_* that's the trigger of Laravel auth to determine if the authenticated user was remembered.

 

Download Source Code

 

Thanks for reading. If you think this tutorial is helpful to you. Please share it with your friends.

 

Happy coding :)