Table of contents
In this post, I will share with you how to implement a remember me function with custom time expiration to your Laravel login.
Â
UPDATED IMPLEMENTATION VISIT HERE.
Â
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.
Â
Â
Thank you for reading. If you think this is helpful please share it with your friends.
Read next