DEV Community 👩‍💻👨‍💻

Cover image for How To Add Remember Me Functionality To Your Laravel 8 Login?
Code And Deploy
Code And Deploy

Posted on

How To Add Remember Me Functionality To Your Laravel 8 Login?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-add-remember-me-functionality-to-your-laravel-8-login

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.

how-to-add-remember-me-functionality-to-your-laravel-8

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
Enter fullscreen mode Exit fullscreen mode

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

cd login-with-remember-me
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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.

how-to-add-remember-me-functionality-to-your-laravel-8

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);
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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

Now let's try if working.

how-to-add-remember-me-functionality-to-your-laravel-8

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.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-add-remember-me-functionality-to-your-laravel-8-login if you want to download this code.

Happy coding :)

Top comments (0)

Want to Create an Account?
Now it's your turn!
 
🗒 Share a tutorial
🤔 Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.