How To Add Remember Me Functionality To Your Laravel 8 Login?
Tags: how, laravel, php, authentication
Published: 2022-03-24 12:49:12

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

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)


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](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-add-remember-me-functionality-to-your-laravel-8-login-HLuKgxXMAix980.webp?ctm=1630198841)


In my previous post, I posted on [how to log in and register with email and username](https://codeanddeploy.com/blog/laravel/laravel-8-authentication-login-and-registration-with-username-or-email) 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.

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

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

```php
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.

```php
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](https://codeanddeploy.com/blog/laravel/laravel-8-authentication-login-and-registration-with-username-or-email).


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.

```php
<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.

![how-to-add-remember-me-functionality-to-your-laravel-8](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-add-remember-me-functionality-to-your-laravel-8-login-xVVSpkgRMVx980.webp?ctm=1630197983)

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:

```php
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:

```php
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.

![how-to-add-remember-me-functionality-to-your-laravel-8](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-add-remember-me-functionality-to-your-laravel-8-login-ai3x9DWr9Kx980.webp?ctm=1630198247)

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.

[![Advanced Laravel SAAS Starter Kit with CRUD Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyjyev8tz3r8vi2w3on1.png)](https://1.envato.market/5g5QAn)

[Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!](https://1.envato.market/5g5QAn)


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 :)