简体   繁体   中英

Automatically redirect to login page when session timeout in laravel 5.4

I'm working on Laravel 5.4. Without making a request pages must redirect to login page when session is timed out. I don't want any Javascript code. I tried but its not working well for multiple tabs. I even tried with Middleware but that did not work. References I tried:

Is there any solution to solve this issue in Larvel 5.4 framework, directly redirect to login page after session timeout.

You can catch the 419 error in the render function of App\Exceptions\Handler.
Something like this

if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode() == '419') {
    //your custom handler here
}
return parent::render($request, $exception);

Try this

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        'App\Http\Middleware\Authenticate',// add this line according to your namespace if not found in your kernel.php
    ];


you just add your route in this middleware

Route::get('/admin-someting', ['middleware' => 'auth', function () {
    //your route
}]);

or this put on your controller

public function __construct()
{
    $this->middleware('auth');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM