Redirecting to Login Page upon Session Timeout in Laravel 5.4 – Paraphrased

Feedback


Solution:

Your Exception Handler is responsible for managing all requests that are not authenticated and processing them according to your requirements.

It will look something like this:

This pertains to a file named “Handler.php” located in the “Exceptions” directory of the application.

/**
 * Convert an authentication exception into an unauthenticated response.
 *
 * @param  IlluminateHttpRequest  $request
 * @param  IlluminateAuthAuthenticationException  $exception
 * @return IlluminateHttpResponse
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }
    return redirect()->guest('login');
}

Modify the redirection by replacing the code

return redirect()->guest('login');

with

return redirect()->guest('backoffice/login');

.

Frequently Asked Questions

Posted in Php