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

bhojkamal's avatar

Redirect to custom URL after session Timeout in Laravel 8

Hello,

I want to redirect to custom the URL after the authentication user session Timeout or for the guest users if they put the dashboard path directly on address bar.

For now the default path is /login if the authenticate user session expired or guest user put the path of dashboard or auth-path.

I want to redirect to just / or suppose own custom login page like /admin/login. I set false to the default login router web.php as well.

I will appreciate for the solution.

0 likes
5 replies
SilenceBringer's avatar

@bhojkamal go to the app/Http/Kernel and find

'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

Create you own Authenticate middleware which extends \Illuminate\Auth\Middleware\Authenticate. and rewrite redirectTo method to the url you need.

4 likes
rktaxali's avatar

@SilenceBringer Could you please explain, how to "create you own Authenticate middleware" and how to use (define) it. My basic need is to display a page that displays a message that the user has been logged out due to inactivity instead of displaying the 'login' page

Snapey's avatar

you want to redirect the user when they try to interact with the site, or the page to change on its own when the session expires?

bhojkamal's avatar
bhojkamal
OP
Best Answer
Level 1

Hello,

I found a solution for it.

I went to App\Http\Middleware\Authenticate removed the route('login') and put own route. as below.

protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('admin');
        }
    }

I have defined the route for admin in web.php.

1 like

Please or to participate in this conversation.