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

wizjo's avatar
Level 26

Change default login redirect url

I protect my admin routes using middleware auth in my routes/web.php file :

Route::group(['middleware' => ['web', 'auth'], 'prefix' => 'admin'], function () {
    //... all admin routes
});

Now when Im trying to access any admin route being not logged in Im redirected to: http://localhost:8000/login. How to customize that login url?

0 likes
9 replies
Cinek's avatar

In Exceptions/Handler.php override the unauthenticated method.

rumm.an's avatar

This is the method from Laravel's Handler Class

/**
     * Convert an authentication exception into a response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        return $request->expectsJson()
                    ? response()->json(['message' => $exception->getMessage()], 401)
                    : redirect()->guest(route('login'));
    }

You can override this method in your App\Exceptions\Handler class, to change the route.

rin4ik's avatar

like @Cronix always says don't override vendor files. because when you update composer all your data will be deleted.

1 like
wizjo's avatar
Level 26
  1. Is updating app/Exceptions/Handler.php safe and after doing composer update it won`t be overwritten?

  2. I see this method use route named login, so I wonder why it is not redirecting to my custom route which has the same name?

routes/web.php:

Route::get('logowanie', 'Auth\LoginController@showLoginForm')->name('login');
rin4ik's avatar

yes it should redirect @wizjo maybe u use default auth?. run please php artisan route:list and see your login route

wizjo's avatar
Level 26

It is strange because php artisan route:list shows that login route points on Auth\LoginController@showLoginForm

rin4ik's avatar

@wizjo yes that's strange please try maybe this commands can help

composer dump-autoload
php artisan route:cache

hard refresh the browser(ctrl+f5) and try again redirecting

rumm.an's avatar

@rin4ik We dont need to change any vendor files. We just need to override a method in our Handler class. I gave the example from vendor file to show how the method looks like. You can copy this method in your own app\Exceptions\Handler.php class and modify it to redirect to a specific route.

Cronix's avatar

It is strange because php artisan route:list shows that login route points on Auth\LoginController@showLoginForm

Check the routes again. There are several auth routes. One displays the form (like quoted above) using a get request, and another is for posting the form.

Please or to participate in this conversation.