Laravel 7 redirect different pages users after login. Admin and User login redirect different page.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back to this channel so in this video I would like to show you how to create different three directions after they after the user logs in for example they're using the same login page and let's say just a simple user tried to logs in [Music] entered a logged in and they got redirected to the user dashboard they are logged in as a user if they try to access to the admin page for example they are not allowed they have they haven't got the access to this they haven't got the right to access to this page if I log out and I'm logged in as a using the same login page I'm logged in as a super administrator or admin or whatever you want administrator in my case as you can see I get redirected to the admin dashboard you are looking as a as an admin so if this is what you're looking for then leave a like please subscribe and let's just start it so in the last video I created this Lara well seven application so it's a brand new installation of thought of s7 with tailwind CSS on the front end and we are already created one user as you can see in my database I have one test user okay so by default if you log in we can't rewrite get to this home page as you can see we have this dashboard and it says you are logged in and if you log out you are not able to access to this page so if you if I am just logged out and if I try to access to this home again we get redirected to the login page we are because we are not not long so if you go to your to your application and if you check your out web dot PHP as you can see you have this round cat slash home and it's direct you to the home controller index method if you go to this home controller in your application app HTTP controller's home controller you got this middleware out which is detect that you are not logged in and it's gets redirected you back to the login page so we would like to do two different pages one for the admin and one for the user so let's just create the route for them I copy it and paste it twice 1:40 user want to go to the user controller at the index and route via the user the other one is admin I want to go to the admin controller index method again and let's name it admin these two controllers are not exist yet so let's just create these two controllers quickly let's go to your project folder open up a console inside us and let's create our user controller so nothing special here PHP artisan make controller user controller hit enter it's created and let's create one for one other one for the administer the tenter and these are created let's go back to be as code we have the admin controller and the user controller and if you go back to our routes web we would like to go to the user controller index method and the adven controller index method again so let's go back to the user controller and let's just quickly add this index method or even you can you can copy it from your home controller now we are in our user we have the index mentor and I want to return the not home you but I would like to show the user index page for example same with admin if you go to the admin controller you would like to hit the index method and you would like to return a view from the admin folder so let's just create this folder and the index file same with the user create the user folder and the index file scroll a bit down go to the resources and if you go to the views that's right click on the views folder and add a new folder one for the admin and the other folder for user and inside the user folder let's create a new file called index dot blade dot PHP and inside I mean folder create the same index dot later dot php' hit enter and basically i want the same look as as it is now so if you go to the home dog blade as you can see we have this dashboard here dashboard you are logged in I want exactly the same for my user [Music] but I want to say that use your dashboard and to say you are you logged in as a user same for the admin dot index of blade this will be our admin dashboard and you are logged in as an admin okay everything is automatically saved now we need to install a packages for for that you don't need to install the packages but I will install a package is called a lot of trust so if you type in Google you will see this lot of trust and the newest version is 5.2 and for this we will use a package called lot of trust so if you type in to Google a lot of trust you will find it and latest version is 5 points too and the reason you can do it you can do this logic be happy without this lot of trust the package but the reason I like this a lot of trust package because you can easily attach rows to different users in your application and you can check easily if they are are mean if they are just simple user or you can give different positions to users in your application so there could be just a basic user and blog writers and super administrator who can control everything on your page so you can set different levels on your page and you can manage it and control it easily with this laga trust package as you can see if you have for example a new user you can make it administrator way so you have a new user let's say and you can say at a troll admin and from them that user will be alman on your page but of course by default I will set it to just the simple user so anyone who register on the page will be just a simple user without any special permissions ok so let's just jump in and install this package for our application you go to the install installation tab is a pc composer require let's just copy it we are inside our blog project let's copy it again based hit enter it will install this package to your application okay that's installed nothing special here let's just follow the steps we can leave the step two step three out because we are using a lot of air seven excellently we need to publish these files so copy this step for paste enter that's done I just do this config clear as well probably it would indeed but I just do it des demands well okay next step configuration so you can see this tab after installation if you click on it there is a manual way or if you scroll a bit down or if you select this automatic setup as you can see it's recommended and I recommend it as well so I just copy this PHP artisan lava trust setup command and paste it inside our command line enter let's type in yes hit enter as you can see it's just created our migration files raw permission and largest rate after that do PHP artisan conflict clear again enter and composure out dump and composure down alter load as well based enter right debts done as well let's just go back to the we escort for a second you [Music] I just go back to the BS code for a second so now if you go to the config file you should see a lot of trust cedar and letter trust as well so you can see all the free motors so user could have rows permissions and stuff like that and if you go to the largest cedar you can see we have by default it's coming with the trans by default you have a basic user you have a administrator and you can have a super administrator who controls the site basically but if you want you can add more different levels so if you want you can add for example a blog writer just copy one of these and say you can name your levels whatever you want so let's say blog writer and it will create your blog writer automatically as well let's just delete it and close this okay and the other one if you go to the database you will see we have a new migration Y which will create everything in your database to set up these relationships between the user and the permissions and roles and stuff like that and if you go to the database seed we don't have cedar out of the box for this but we will install a cedar as well it's very useful so if you and by the way if you go to this model user it says you need to add this line to your user model but if you check its funded automatically you have this lot of trust you was a trade so you don't need to do that as well alright so let's go back to this page you will see we could create this PHP artisan alert last user let's just do that as well paste enter cedar successfully created if our if we check our application database migration seeds you can see we have this letter trust cedar now so it will create one just a normal user a administrator and the super administer super user super administrator as well in our database so we don't know we don't have to do that by manually but we need to specify this action in our database cedar so if you go back to the website you can see we have to do composer all too low or we have to do composer dump auto load again and right dad's done now let's go back to the letter trust website and we need to add this line to our database cedar so copy it go back to our application and we are inside the migration we are inside the database see there folder and database C dot PHP we just need to add in this line it's saved automatically and now if we are if you are check our database so that's where we are we had a test user in our database but let's just migrate our new files into this database so let's type in PHP artisan migrate it enter as you can see we have this permission rolls now and permission users and roles and permissions table but now let's see it with by basic setup so let's say PHP artisan DB seed okay that's done if we check out ADA base hit refresh if you check your user database as you can see our test user has been deleted and we have four type of user now we have a super administrator and as you can see it's automatically generated an email address as super administrator at app.com and we have just a simple user so this is our simple user with a simple user @ app calm email address and it's automatically generated a password for each user and the password is password that's it so now no capital letters nothing just password this is the password it's a mattock we generated of course you can update these passwords or you can create new users and of course you can add your password accordingly and as you can see we already populated our roles and permissions table so everything is populated for you so it's working out of the box okay but now if we are going to if you are trying to log in like for example if you try to log in as a super administrator we type in our password which is password as I just mentioned hit enter we still get redirected to this home route and it would be the same if I try to log in as a user so let's just sort this out we have already created these routes user admin and we already created these controllers for them as well now we need to go to this app folder HTTP folder and out folder and if you go to the register controller this is how a lot of I'll register our users into our databases so this is the validation it's check if we have name email and password typed in and then is create our user but from now we would like to attach a role for each registered user and by default I want a newly registered user just simple user so I don't want to allow to access different pages on my website for example I don't want a freshly created user to have access to my admin page for example in order to do that it's as you can see it's creating a new user on the fly and it's on the fly returning the newly created user but I want to create a user variable here which will be equalled equal to this newly created user and then and I will copy it from my existing project and then as I just mentioned earlier in this video easily you can attach different roles to users so this newly created user I want to make it just a simple user so this user attached raw user but you can make it for example admin or blog writer or whatever you want but by default I want to create this user and then I return this newly created user and we want to redirect them at the moment we are directing to the home page but we like we haven't got this from home page anymore so we would like to redirect them to the user page and you have to do the same for user confirmation password you will need to change this one and for the password reset password as well but first let's create the logging controller and create logic there because you know you need to use the same logic in the reset password and the confirm password controllers as well next one we need to go to the login login controller so as you can see by default we get redirected to the home route if you are login so by default we are getting redirected to this route you can leave it as it is so there is a new user for example if they are logged in they are creating their account then you can direct them to this simple home and then you can for example make them admin or super admin or blog writer and then they get redirected accordingly but for now I just delete this route and I don't want to I don't want to redirect any of my user to this home route I want to direct them to my user page what I just created in order to do that we need to use a request as well well let's let me just copy and paste it here so make sure you have this illuminate HTTP request imported up in your file and we have deleted this line and here is Val you would like to check if your user is super administrator blob writer or whatever you want or just a simple user and then redirect them accordingly I will copy and paste this so basically just place this if statement and you check if you're currently logged in or currently registered user has a role super administrator or admin or blog writer then get related to the admin page if not then check if the user has a ro user provider whatever you want and then redirect them accordingly so in my situation if the user has super administrator then I Ross or administrator then I want to provide it to the admin page if the user just a simple user I want to redirect them to the user dashboard page ok let's test this out so as you know because we use the database seed we have super administrator user with this email address and password and we have just a simple user with this email address and with a password so let's try it out if I log in as a normal user I type in this email address and the password is password click login as you can see I got redirected to the user dashboard you are logged in as a user if you log out and try it out with your super administrator with your admin super administrator email address and password is password okay it works you got redirected to the admin dashboard but we have one more problem the one problem is that I can access to the user profile page so as you can see I typed in user and I'm on the user dark word page and vice versa if I logged in as just a simple user and if I type in admin hit enter I could access to the admin page as well and the other problem is that if I looked out a jar as I just did as you can see I'm logged out and if I try to access for example for the admin page iego I'm still have access to this page even though I'm not logged in so let's just fix it quickly if you go back to your vs code have two controllers currently we have a admin controller and a user controller so if you check the homer home controller by default you have this constant function construct which will check if the user is authenticated and if it's not out that thing authenticated then its get redirected back to the login page so we will use this public function function construct in our user controller and in our admin controller by just doing this this is the default love of l1 so I copied it from the home controller okay so by doing this if I try to refresh so now if I try to access for the admin page I could rewrite it to login and let's say I try to access to the user page it was a profile page I could try delighted to log in but I still have a problem if I'm logged in as a log in as a user password login you okay I got to rewrite it to the user dashboard but I'm still have access to the admin page and this is where a lot of trust comes in to play so if you go back to we s code I'm in my admin controller and we are checking if the user is authenticated but I also want to check if this user is just a simple user or a super administrator if they are super administrator then I want to give them an access to these pages so you can easily do that by their trust just simple typing in role and super administrator and if they are super administrator then they have access to this page if not big utley directed and you have to do same with the user [Music] if you don't want to give access to for example for your super administrator you just type in user and now only the user could have access to this page but the super administrator doesn't out logout login as a simple user okay I'm logged in it was a dashboard try to access to the admin and here you go 4:03 you don't have the necessary rights to visit this page so while you you are not allowed to go to this page you still can within the user page and if you logged out and you try and you log in as a super administrator you are attracted to admin dashboard try to access to user page and you don't have the necessary rights but maybe you want to give access to this user page for your admin and you can easily do that by going back to this and just to use this pipe and type in the next row where you want to give access to this page so for example you could it could be admin or super administrator so let's check it again there you go no I can have access to my users page as well and that's it guys you can test it to tweak it and create your logics and permissions and roles however you want you can see some concepts configurations and all other stuffs on the largest plotter trusts website and you can use them in your application thanks for watching please subscribe bye
Info
Channel: Code Fisher
Views: 57,417
Rating: undefined out of 5
Keywords: Laravel 7, Laravel 7 multi auth, aravel multi authentication, Laravel 7 admin and user dashborad, How to redirect user and admin to different pages after login, laravel login, programming, web development tutorials, programming tutorials, laravel tutorials, Coder's Tape, Traversy Media, Dev Ed, laravel for beginners
Id: _YXKLpOpJY0
Channel Id: undefined
Length: 27min 52sec (1672 seconds)
Published: Sun Apr 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.