Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 25, 2022 04:38 am GMT

How To Change The Laravel Redirect URL When Not Authenticated?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-change-the-laravel-redirect-url-when-not-authenticated

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:

how-to-change-the-laravel-redirect-url-when-not-authenticated

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');    }}

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

return route('login.show');

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 https://codeanddeploy.com/blog/laravel/how-to-change-the-laravel-redirect-url-when-not-authenticated if you want to download this code.

Happy coding :)


Original Link: https://dev.to/codeanddeploy/how-to-change-the-laravel-redirect-url-when-not-authenticated-2p26

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To