How do I change “remember me” expired time in Laravel?
In this article we will discuss how you can easily customize the default exit time for remember me token in Laravel. We would also get to know the default value for the remember me token expiration time in Laravel.
As you can see, it is set to five (5) years by default and it is literally huge and therefore you may want to customize it to your project and we will see how you do it in this article.
You can easily see that the LoginController.php uses AuthenticatesUsers.php attribute and you just need to override a way of this trait into your LoginController.php file.
Now, override this feature and do this, just copy the function sendLoginResponse(Request $request) and just add it to your LoginController.php. Your LoginController.php would look something similar to this:
Here, the $ customRememberMeTimeInMinutes variable will have a value equal to the memory token expiration time you want to set (In Minutes). After doing this, simply try to log in to your project while selecting the Remember Me check box. You will be able to see that the new memory token expiry time is now only 10 minutes instead of 5 years.
Introduction:
As we all know, from 2019 Laravel is a leading PHP framework and is higher in the list of best PHP frameworks (2019) and many use it for their projects. Now suppose that in your project you use Laravel's php craftsman: authent to manage authentication-related aspects and now you want to manually adjust the expiration time according to your project needs. To handle this, we would see the procedure by which you can easily change the default value of the remember me token expiration time in Laravel.The default expiration time for remember me token in Laravel:
Before we proceed to customize the memory token expiry time, let's see the default memory-token expiry time.As you can see, it is set to five (5) years by default and it is literally huge and therefore you may want to customize it to your project and we will see how you do it in this article.
Stepwise Procedure to Modify the token's Expiration Time:
For example, suppose you set the expiration time of remember-me token to 10 minutes. Below is the step wise procedure you can follow.[Customizing token's Expiration Time] Step One:
Head over to app/Http/Controllers/Auth/ in your project's directory and open LoginController.php file, you would see something similar to this:<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller maintain authenticating users for the application and | redirect them to your homepage. The controller uses a speciality | to easily provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } }
You can easily see that the LoginController.php uses AuthenticatesUsers.php attribute and you just need to override a way of this trait into your LoginController.php file.
[Customizing token's Expiration Time] Step Two:
In AuthenticatesUsers.php trait, you would find a protected method sendLoginResponse something similar to this:/** * Send feedback after user is authenticated. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ protected function sendLoginResponse(Request $request) { $request->session()->regenerate(); $this->clearLoginAttempts($request); return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath()); }
Now, override this feature and do this, just copy the function sendLoginResponse(Request $request) and just add it to your LoginController.php. Your LoginController.php would look something similar to this:
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cookie; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller maintain authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | To provide its functionality to your applications easily. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function sendLoginResponse(Request $request) { $request->session()->regenerate(); $this->clearLoginAttempts($request); return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath()); } }
[Customizing token's Expiration Time] Step Three:
Now, In your LoginController.php file, Just replace the definition of sendLoginResponse() method with this:protected function sendLoginResponse(Request $request) { $customRememberMeTimeInMinutes = 10; $rememberTokenCookieKey = Auth::getRecallerName(); Cookie::queue($rememberTokenCookieKey, Cookie::get($rememberTokenCookieKey), $customRememberMeTimeInMinutes); $request->session()->regenerate(); $this->clearLoginAttempts($request); return $this->authenticated($request, $this->guard()->user()) ?: redirect()->intended($this->redirectPath()); }
Here, the $ customRememberMeTimeInMinutes variable will have a value equal to the memory token expiration time you want to set (In Minutes). After doing this, simply try to log in to your project while selecting the Remember Me check box. You will be able to see that the new memory token expiry time is now only 10 minutes instead of 5 years.
How do I change “remember me” expired time in Laravel?
if you want to know more about PHP, then you visit PHP training in Chandigarh, sector 34a.
|
- Get link
- X
- Other Apps
Labels:
laravel
php training in Chandigarh
- Get link
- X
- Other Apps
Comments
Post a Comment