How To Implement Remember Me with Custom Expiration To Your Laravel 8 Login?
Tags: how, laravel, authentication, php
Published: 2022-03-24 13:05:43

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-implement-remember-me-with-custom-expiration-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 share with you how to **implement a remember me function with custom time expiration to your Laravel login**.

 
In my previous post I share about remember me with details now we will tackle how to set the custom expiration. Because in default the Laravel gives 5 years of expiration as you can see below:

![how-to-implement-remember-me-with-specific-expiration-to-your-laravel-8](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-implement-remember-me-with-specific-expiration-to-your-laravel-8-login-IMkV7h0de9x980.webp?ctm=1630199289)

Okay, let's start.


## Step 1
Please follow my [previous post](https://codeanddeploy.com/blog/laravel/how-to-add-remember-me-functionality-to-your-laravel-8-login) because I'm using it to shorten this post and simpler.

 

## Step 2
Let's do it with **Traits** for more info please visit the PHP documentation [here](https://www.php.net/manual/en/language.oop5.traits.php).

 

## Step 3
We will create a new folder named "**Services**" inside the **App** folder in the Laravel directory project.

 

## Step 4
Create folder **"Login**" inside "**Services**" folder then create a file `RememberMeExpiration.php`

 

## Step 5
Then copy this code below:

```php
<?php

namespace App\Services\Login;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;

trait RememberMeExpiration
{
	/**
     * Set default minutes expiration
     *
     * @var int
     */
	protected $minutesExpiration = 43200; //equivalent of 30 days

	/**
	 * Customize the user logged remember me expiration 
	 * 
	 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
	 */
	public function setRememberMeExpiration($user) 
	{
		Cookie::queue($this->getRememberMeSessionName(), encrypt($this->setRememberMeValue($user)), $this->minutesExpiration);
	}

	/**
	 * Generate remember me value
	 *
	 * @return string
	 */
	protected function setRememberMeValue($user) 
	{
	    return $user->id . "|" . $user->remember_token . "|" . $user->password;
	}

	/**
	 * Get remember me session name
	 *
	 * @return string
	 */
	protected function getRememberMeSessionName() 
	{
	    return Auth::getRecallerName();
	}
}
```

As you can see above I have a protected property called **$minutesExpiration** you can customize it depends on your needs.

 

## Step 6

Let's use the **RememberMeExpiration** trait in our **LoginController**

 

## Step 7

This is the complete LoginController code:

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\LoginRequest;
use Illuminate\Support\Facades\Auth;
use App\Services\Login\RememberMeExpiration;

class LoginController extends Controller
{
    use RememberMeExpiration;

    /**
     * Display login page.
     * 
     * @return Renderable
     */
    public function show()
    {
        return view('auth.login');
    }

    /**
     * Handle account login request
     * 
     * @param LoginRequest $request
     * 
     * @return \Illuminate\Http\Response
     */
    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'));

        if($request->get('remember')):
            $this->setRememberMeExpiration($user);
        endif;

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

    /**
     * Handle response after user authenticated
     * 
     * @param Request $request
     * @param Auth $user
     * 
     * @return \Illuminate\Http\Response
     */
    protected function authenticated(Request $request, $user) 
    {
        return redirect()->intended();
    }
}
```
As you can see above code inside **login()** method I added these 3 lines of code below:

```php
if($request->get('remember')):
    $this->setRememberMeExpiration($user);
endif;
```

We call the **setRememberMeExpiration()** method from **RememberMeExpiration** trait.

![how-to-implement-remember-me-with-custom-expiration-to-your-laravel-8](https://ecn-storage.s3.us-west-2.amazonaws.com/articles/how-to-implement-remember-me-with-custom-expiration-to-your-laravel-8-login-afNO0WbZ1rx980.webp?ctm=1630200867)

Now we already customized our remember me expiration I hope this tutorial can help you. Download the complete source code below for you to test and use it.

[![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-implement-remember-me-with-custom-expiration-to-your-laravel-8-login if you want to download this code.

Happy coding :)