Laravel 10 Redirect Admin and User to different dashboard Route or redirect to the same. #laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back to this Channel today I will show you how to use the same login form and same register form and redirect the user to the different route for example the admin will be redirected to the slash admin dashboard and the normal user will be redirected just to the simple dashboard SL dashboard route and then I will show you how to use the same login and register form and redirect the admin and the normal user to the same dashboard route but serve them different content they will see different content on the dashboard basically and if you're interested how we made this to-do app in lell 10 with different roles and permissions you can check the previous video link link will be in the description now before we jump on the code please subscribe to the channel and hit the like button okay so in l 10 everyone who logs in or register gets you directed to the dashboard Page by default and that's this SL dashboard comes from uh the following file if you go to the providers folder and the route service provider you can see this public const home and this is uh redirect us to the dashboard so for example if somebody is authenticated so logs in after the logs in they will get redirected to this Home Route which is/ dashboard so as you can see the redirection uh uh value gets from this route service provider home constant so if you go to the route service provider home same if you register a if the new user is registered after we created the new user authenticate the new user and it's get redirected to the route service provider home so everyone gets redirect Ed to this uh to this/ dashboard so if you would like to redirect your admin and the user to the different uh routes after they logged in then you need to do some modifications unfortunately I'm not able to change as far as my knowledge I'm not able to put some logic here to decide if the uh user is U admin or just a simple user and redirect them accordingly so what I will do I will go to the users uh model and just create a new function get redirect route and in this function as we are using uh lat trust we have this uh method has roll so we can check if the user has a admin role or to in our case we create a to-do list user which is basically a simple user so we have admin and to-do list user and if we have an admin who logs in then we want to redirect them to the admin dashboard uh or if uh the logged in user is just a simple user then we want to use uh the user dashboard uh route okay so inside this user.php we are checking what role the user has and we redirect them accordingly we are create creating this route for them so now we need to use this uh function whenever a user logs in so if you go to the app folder HTTP controllers out folder then inside that you will see this authenticated session controller and and here you will see this uh line where you need to use this newly created function so we check the authenticated user get redirect route so we running this uh function and also if you if we register a new user we also need to change here as well we need to check the authenticated user uh get redirect route of course don't forget you need to maybe change in your application in different uh places but for now we change it in inside this u login and register controller okay now we need to create these uh routes admin dashboard and user dashboard so let's go to the web.php and for now we just a simple copy and paste this route two times and let's make admin dashboard and a user dashboard and for the admin dashboard we would like to serve we would like to serve the uh admin dashboard view same for the user now we need to create these views so if you go to the view uh if you go to the resources views we need to create basically we copy and paste this dashboard. plate. PHP so let's just uh copy and paste here and one more time so let's rename this okay so now we have an admin and a user dashboard Let's uh just modify this uh admin dashboard you are logged in as an [Music] admin and now let's create a new user let's L out register a new user and by default we are creating a users so that's why we get redirected to the users page as you can see user dashboard and uh we already have created a admin user in this application so if I log out and log in as an admin admin dashboard not found I think I have a typo somewhere uh AD mean dashboard okay let's check the code yeah okay so it's immediately works now okay so I'm logged in as an admin now and I get redirected to the ad admin dashboard okay so this is how you can redirect the admin and the user uh to different routes and serve them different content uh but they can still use the same register and login form now let's uh use the same login and register form and redirect back them to the dashboard but show them different content according their RS so the admin will see admin dashboard and the user will see user dashboard but they both of them uh red get reeded to the dashboard so the route will be the same but the content is different we just created different routes with different content now we do the same route with different content so for this I will roll back everything here because uh we want to use this route service provider so register has to be rolled back as well okay and the authenticated user get redir to the home okay so now we will be redirect our user as its default in LEL so we redirect them to the home which is the/ dashboard inside the route folder webp so all the user who registers or logs in we will get will redir to this/ dashboard but they will see a different content for that we will use a dashboard controller or even we can use this uh profile controller it's already there in l 10 so we want to visit the/ dasboard route but after that um instead of this uh instead of of this uh custom function let's just delete it and we want to use this controller let's make uh a custom method inside this controller called dashboard okay the name will be the same and of course they have to be authenticated and verified okay so now if the user logs in or register will be get redirected to this dashboard page which will serve basically which will goes to this profile controller dashboard method now let's go to the HTTP folder controllers and you will see this uh profile controller by out of the box it's came come with comes with the rare Breeze and let's just create this dashboard method here public function basically okay so now inside this dashboard method we will need to check if the authenticated user who just logged in or who just uh created a new account is admin or they are a just a simple user we will use here larat trust method has Ro and we can check if the authenticated user is an admin then return a view for them called admin dashboard MH I have a typo here as if so you can check multiple multiple roles we could use only lse here but let's just use l if same check but we want to check if it's a to-do list user in our case so this to-do list user and admin comes from the database uh in the previous video we are uh we are set it up L trust and we are Crea basically if you go to the we are basically created two type of user admin and to-do list user if you go to the uh config folder which should be here yeah config and L Ceder as you can see I created an admin and to-do list user so I have to two type of users okay so now let's test this out we already receive a problem profile profile controller dashboard man meod doesn't exist check it HTTP controllers profile controller oh yeah I have a typo here okay let's see should work [Music] now yes okay so at the moment we are logged in as an admin so we are seeing the SL dashboard same route but with different content here now let's log out and create a new user which will be automatically will be a just a simple to-do list user because that's how we create a new user so if you check the app HTTP and authentication and register new user as you can see when we create a new user we add a role to-do list user so out of the box all the new user who registers will be this to-do list user so we should see user page but we are see nothing so dashboard which could be another typo here if you go to the profile controller oh yeah because we are not um returning anything for this uh for this if statement so let's just do that so else if authenticated user has a to-do list user role then return a view I forget this part but at this time I want to show them the user dashboard uh content let's see now if I hit refresh as you can see I'm logged in as a simple user if I log out and log in as a admin and as we expected we are at the admin dashboard page now at the dashboard route register new to-do list user logs in same route dashboard but we are showing them different content okay I hope this content helped to you if so please hit the like button and subscribe to this Channel and see you soon in the next video
Info
Channel: Code Fisher
Views: 821
Rating: undefined out of 5
Keywords: laravel, laravel 10, laravel 10 admin and user roles, laravel 10 roles and permissions, laravel 10 middleware, laravel 10 rediect admin and user to different dahsboard, Laravel 10 show different view to admin and user but same route
Id: NJMc7asJzms
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Sun Nov 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.