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?
In Exceptions/Handler.php override the unauthenticated method.
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.
like @Cronix always says don't override vendor files. because when you update composer all your data will be deleted.
Is updating app/Exceptions/Handler.php safe and after doing composer update it won`t be overwritten?
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');
yes it should redirect @wizjo maybe u use default auth?. run please php artisan route:list and see your login route
It is strange because php artisan route:list shows that login route points on Auth\LoginController@showLoginForm
@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
@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.
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 sign in or create an account to participate in this conversation.