DEV Community

Cover image for How To Change The Laravel Redirect URL When Not Authenticated?
Code And Deploy
Code And Deploy

Posted on


How To Change The Laravel Redirect URL When Not Authenticated?

Originally posted @ visit and download the sample code:

In this simple post, I will share with you how to change the Laravel default redirect URL when not authenticated or not log in? The default Laravel installation is the login route named "login" but if you change it it will cause an error when visiting the page with an unauthenticated account. See sample below:


To fix this issue when naming your login with a different name we need to update the Authenticate class which we can found in this directory App\Http\Middleware and you will see this default below:

* Get the path the user should be redirected to when they are not authenticated.
* @param  \Illuminate\Http\Request  $request
* @return string|null
protected function redirectTo($request)
    if (! $request->expectsJson()) {
        return route('login');
Enter fullscreen mode Exit fullscreen mode

Next, we will change the login to your login name route, mine is "".

return route('');
Enter fullscreen mode Exit fullscreen mode

And after updating the code it will not occur the error when visiting the restricted routes for unauthenticated users.

I hope this tutorial can help you. Kindly visit here if you want to download this code.

Happy coding :)

Top comments (0)

50 CLI Tools You Can't Live Without

The top 50 must-have CLI tools, including some scripts to help you automate the installation and updating of these tools on various systems/distros.