Laravel 8 Auth Redirection Using redirectTo

Laravel 8 Auth Redirection Using redirectTo

In this post we will give you information about Laravel 8 Auth Redirection Using redirectTo. Hear we will give you detail about Laravel 8 Auth Redirection Using redirectToAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we’ll learn how to customize the auth system in our Laravel 8 app to redirect users after they register or login to a different route depending on their role.

Most of the times, the authentication system provided by Laravel 8 is enough for adding login and registration to your web application.

The auth scaffolding which is now moved to a separate laravel/ui package provides out of the box routes and views for the LoginController, RegisterController, and ResetPasswordController which are included in your project and are responsible for providing the functionality of the auth system.

Please note that the Laravel team recommends developers to use Jetstream for new Laravel 8 projects but they have also updated the laravel/ui package to version 3 for using with Laravel 8, especially if you are updating your previous Laravel 7 app to the latest version.

Laravel 8 Auth Redirection Using $redirectTo

If you take a look at the app/Http/Controllers/Auth/LoginController.php file, for example, you would find the following code:

<?phpnamespaceAppHttpControllersAuth;useAppHttpControllersController;useIlluminateFoundationAuthAuthenticatesUsers;classLoginControllerextendsController{useAuthenticatesUsers;protected$redirectTo='/home';publicfunction__construct(){$this->middleware('guest')->except('logout');}}

You can see that a $redirectTo variable exists and has the value of /home where users are redirected after they are logged in.

In the Laravel built-in authentication system, you can customize many sides such as the redirection route using the $redirectTo variable which exists in both the login and registration controllers.

If you want to redirect your users to different routes other than the default ones after they register or login, you simply need to change the value of $redirectTo.

Laravel 8 Auth Redirection Using redirectTo() Method

Now, what if you want to redirect users to a route depending on some user criteria such as their role?

The Laravel auth system also covers that by providing a redirectTo() method that you can use instead of a $redirectTo variable.

Let’s take this example of the LoginController of our CRM application by adding the redirectTo() method to redirect the admin users to a different route other than the /home route:

<?phpnamespaceAppHttpControllersAuth;useAppHttpControllersController;useIlluminateFoundationAuthAuthenticatesUsers;classLoginControllerextendsController{useAuthenticatesUsers;protected$redirectTo='/home';protectedfunctionredirectTo(){if(auth()->user()->role=='admin'){return'/admin';}return'/home';}publicfunction__construct(){$this->middleware('guest')->except('logout');}}

We also need to do that in the registration controller. Open the app/Http/Controllers/Auth/RegisterController.php file and update it as follows:

<?phpnamespaceAppHttpControllersAuth;useAppUser;useAppHttpControllersController;useIlluminateSupportFacadesHash;useIlluminateSupportFacadesValidator;useIlluminateFoundationAuthRegistersUsers;classRegisterControllerextendsController{useRegistersUsers;protected$redirectTo='/home';protectedfunctionredirectTo(){if(auth()->user()->role=='admin'){return'/admin';}return'/home';}publicfunction__construct(){$this->middleware('guest');}protectedfunctionvalidator(array$data){returnValidator::make($data,['name'=>['required','string','max:255'],'email'=>['required','string','email','max:255','unique:users'],'password'=>['required','string','min:8','confirmed'],]);}protectedfunctioncreate(array$data){returnUser::create(['name'=>$data['name'],'email'=>$data['email'],'password'=>Hash::make($data['password']),]);}}

You can either remove the $redirectTo variable or leave it as it will be simply overridden by the redirectTo() method.

Creating a Laravel 8 Controller for Admin

Now, all you need is to create an /admin route along with an AdminController. Head back to your terminal and run the following artisan command:

$ php artisan make:controller AdminController

Next, open the app/Http/Controllers/AdminController.php file and update it as follows:

<?phpnamespaceAppHttpControllers;useIlluminateHttpRequest;classAdminControllerextendsController{publicfunction__construct(){$this->middleware('auth');}publicfunctionindex(){return"Hello, admin!";}}

Next, open the routes/web.php file and add a route to the admin controller as follows:

Route::get('/admin', 'AdminController@index')->name('admin');

Conclusion

In this tutorial, we’ve implemented redirection in our Laravel 8 CRM app so admin users are redirected to a different route while the normal users are redirected to the home route. Redirection doesn’t enforce any security rules because the normal users will still be able to visit the /admin route. We need to prevent that using a middleware which is the subject of the next tutorial.


Hope this code and post will helped you for implement Laravel 8 Auth Redirection Using redirectTo. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US