Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you an example of “How to Prevent Back Button After Logout in Laravel 8”, So you can easily apply it with your laravel 6, laravel 7, and laravel 8 application.
Suppose we developed an application in laravel where we implemented the auth functionality If you observe deeply then you will find this fault while we log out of our application, we redirect to the homepage after logout and session expired, but the main issue is that when you will click to the back button you can see the dashboard page again, and when we refresh the page again, then we jump into the homepage.
We can prevent this issue by using middleware in laravel 6, laravel 7, and laravel 8. We will create a middleware to Prevent Back Button After Logout in Laravel in the application, and we will use that middleware in our route file.
This scenario only happens because of the laravel cache and browser cache. We set up the cache control in our header in the middleware.
Create a Middleware
php artisan make:middleware DisableBackBtn
app\Http\Middleware\DisableBackBtn.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class DisableBackBtn
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response=$next($request);
$response->headers->set('Cache-Control','nocache, no-store, max-age=0, must-revalidate');
$response->headers->set('Pragma','no-cache');
$response->headers->set('Expires','Sat, 01 Jan 2020 00:00:00 GMT');
return $response;
}
}
Update Kernel File :
You need to update your Kernel.php file inside the same App/Http/Middleware directory where you need to add the middleware name as shown below.
app\Http\Kernal.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'slashes' => \App\Http\Middleware\TrailingSlashes::class,
'authCheck' => \App\Http\Middleware\AuthCheck::class,
'disable_back_btn' => \App\Http\Middleware\DisableBackBtn::class,
];
routes\web.php
Route::get('/user-login',[TestRemembermeController::class,'loginform'])->name('user.login');
Route::post('/check-login',[TestRemembermeController::class,'checklogin'])->name('post.login');
Route::group(['middleware' => 'disable_back_btn'], function () {
Route::group(['middleware' => 'authCheck'], function () {
Route::get('/user-dashboard',[TestRemembermeController::class,'dashboard'])->name('user.dashboard');
Route::get('logout', [TestRemembermeController::class, 'logout'])->name('logout');
});
});
After adding the “disable_cache_hit” middleware in the web route file, you cant jump into the dashboard after logout and clicking on the back button because we successfully prevented or disabled the back button cache using middleware in your Laravel application.
Read Also: Implement Remember me Functionality in Laravel 8.