Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

linktoahref's avatar

Redirect to login page after session timeout

I have an a page rendered via a POST request protected by auth middleware. After session time out If I refresh the page it displays The page is expired due to inactivity. Please refresh the page I would like to redirect it to the login page instead. Which file should I modify to achieve the same?

Any help appreciated! Thanks in advance!

0 likes
1 reply
tisuchi's avatar

May be you can create a custom exception render in the App\Exceptions\Handler class (in the /app/Exceptions/Handler.php file).

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    if ($e instanceof TokenMismatchException) {

        return redirect(route('login'))->with('message', 'You page session expired. Please try again');
    }

    return parent::render($request, $e);
}

Please or to participate in this conversation.