Scarwolf Scarwolf - 1 month ago 13
PHP Question

Laravel Change Redirection part if the Auth's session expires

When the session expires my users are getting redirected to

/login
however they should be redirected to
/backoffice/login
, does anyone know in which file I can change the redirection part?

Answer

Your Exception Handler is what takes all unauthenticated requests and handles them in the way you need.

It will look something like this:

app/Exceptions/Handler.php

/**
 * Convert an authentication exception into an unauthenticated response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Auth\AuthenticationException  $exception
 * @return \Illuminate\Http\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

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

You can adjust the redirect by changing return redirect()->guest('login'); to return redirect()->guest('backoffice/login');

Comments