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 - GET YOUR COPY NOW!
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:
Okay, let's start.
Step 1
Please follow my previous post 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.
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
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
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:
if($request->get('remember')):
$this->setRememberMeExpiration($user);
endif;
We call the setRememberMeExpiration() method from RememberMeExpiration trait.
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 - GET YOUR COPY NOW!
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 :)
Top comments (0)