0

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.

3
  • if the session has timed out and the user tries to reach something that has the auth middleware attached, they will get redirected to login ... not sure how you would force a browser to make a request to then have a redirect when the session expires on the server side though
    – lagbox
    Nov 15, 2019 at 8:16
  • You can't dynamically redirect the user when you are not checking if the user is still logged in. You will need some sort of JavaScript
    – Jerodev
    Nov 15, 2019 at 8:19
  • You can also set a meta refresh tag in the page header with the remaining duration of the session if you want to avoid JavaScript.
    – apokryfos
    Nov 15, 2019 at 8:21

2 Answers 2

0

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);
0

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');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.