Laravel 10 multi auth. Redirect users and admins to different pages. Using Laratrust authentication.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome back to this channel in the last two videos the audio was terrible I know that unfortunately I'm not able to fix it now so I have to re record those videos if I want to make it right but now let's concentrate our next topic which is users roles and permissions in l 10 we will use a package called L trust for this this makes it super easy to create rols and permissions in L applications and uh after this video you will be able to login an a user and show different pages for for example for a just a simple user and for the admin for example so basically after they logged in they will visiting the same route SL dashboard but they see different content you serve different content for users and for the admin for example and you can give different roles uh not just roles but different permissions for example for users and for admin and um L trust make it super easy so let's jump on the code but before we do that please subscribe to the channel and hit the like button if you like the content like this okay so this is where we finished our last uh video we have created this lar 10 to-do list application we can register a new user after the user logs in uh they are redirected to the dashboard page and and they can click on to-do list and they can add to-dos their uh list they can view it they can edit it and they can delete it if they want to okay now let's say if you want the newly registered user to be just a simple user and you want in your application a a admin user as well and let's say for the new created user you don't want to show this to-do list menu item let's say that okay let's look out and this is where uh the lat trust comes in to play this is a very good package for handling users roles and permissions in lot of l 10 applications so let's just uh install it if you go to the latas page let's go to the installation you need to use the compos require lar trust okay after that publish the lust files use this command again basically it's just a simple copy and paste uh to install L trust in your application before we uh run the setup uh latas the new uh version has a Administration panel and we want to install the administration panel as well so we will be able uh edit users um roles and permissions and uh we can add the different roles we can delete roles from our database by this UI okay in order to do that let's go to the L t. PHP and uh and enable this admin panel there so let's go to config L trust and if you scroll down actually it's it's already here so it's from up uh it's nearly to the end panel and register and let's say true also you can set where would you like to uh access for this panel I just leave it as default so it's going to be my application so my Local Host SL trust and you will be able to access to this uh uh panel and also you you can say where you want to go back uh from this uh from this panel okay now basically we have enabled it by set this volue to true now let's uh publish the files for that okay and now let's go back for the installation and let's uh do PHP just set up yeah we want to create the migration files as well but before we do a migration fresh we would like to create a cedar so we can preet up basically our application out of the box uh to that go to the cedar menu and let's do PHP Artisan lest Cedar loads of copy and paste okay now if we are going to the config file you will see the lust Ceder PHP and here you can uh set basically your rules and uh permissions up just before we running the cedar so by default this is the uh default values but uh for me I don't need Super administrator I just need admin and a simple user let's say to do list user and I don't need the simple user all the other stuff okay the to-do list user uh is not a I don't want to be able to create uh they will be able to register the page but I don't want them um a permission to create new users other than themselves so basically I can delete this part uh I want them to able to read and update their profile and also I want to be make them able to delete their profile so d stands for delete and of course I want to make them create their to-do list after they log in so let's do too and I want to allow them to create read update and uh [Music] Delete okay and we don't need payments in this application I don't have any payment of course the admin is able to create read update and uh Delete users from the users table okay so basic that's it but you can um customize it however you want it could be blog writer admin editor so you can it's up to your IM imagination and of course this is could be a blog post here and uh the simple user could uh create read update and delete blog post on your site or whatever you want okay now it's done let's go and uh do the migration because at the moment if you check our database we have only one user because we are created one user uh but that's it and we don't have any RWS and permit in our database so let's just uh do this PHP Artisan migrate fresh if we check our database now hit refresh as you can see we have the permissions and roles and uh role and user relationship has been cre created as well at the moment we don't have any user but now if we are creating a new user they are not will be attached to any rols basically they will be out of our um rolls and permission mechanism so if I just register a new user okay they are logged in and they see this to-do list menu item and if we visit the database we have created one new user but they don't have any rols attached to them of course now we have to um um seed our database with the roles and permissions we have just created here so for that we need to implement this part so this line of code has to go to the database seed database seed PHP file so database seeders and database Cedar and inside this run method let's just paste this code and now we are able to seed our database and if we check our database now hit refresh as you can see we have the rules the admin and the do list user and we have all the permissions uh to do create to do read update delete and and all the other stuff here okay but our user with uh ID number one is not attached to any role even if I do the new user now user ID number two is not attached to any any uh role so let's just do that how to do this if you go to the r page and go to the roles and permissions you will see the user roll assignment and removal so if you go to the assignment it's very simple add roll and basically you can use a simple string uh which is can be found in the database if you go to the ruls you see the name admin or to to-do list user you can just uh put admin or to-do list user here and uh it's going to be automatically assigned to that role that user so out of the box I want to assign all the users uh to be able to create a to-do list so they will be a to-do list users but first let's create only one admin user I want to go to the app folder HTTP controllers authentication and registered new user controller and here we validate the user registration and here we need to add a role to the user and now for the very first first user I want to make it uh admin okay so now if I create a new user log out let's make it admin yes the problem is we forget to set up our uh P trust if you go back to the installation as you can see for the for the user model we need to add these couple of couple of things before we will be able to use latr so let's go back to the app folder models and user and we need to paste these two lines here from L trust page also we want to use uh Implement lat trust user and also we want to use it as well so let's go back and has roles and permission has to be used as well okay now if I go back and hit refresh it's already taken so let's just delete this user from the database okay now we are able to save a user so let's just check the database as you can see user ID number four has a roll ID number one so let's check user ID number four is just has been created ID 4 admin uh what have we created and the RO ID one is admin now let's create one more user for just a to-do list ad me to-do list us user register fake filler okay uh I forget to change how I want to save this new user so I want every user from now to be a simple uh to-do list user okay so it's coming from from here to do list user now if if I log out and register a new user we are logged in again and if you go to the users user number six has to be have a role number two and yeah user number six has role number two which is the to list user so from now every user will be automatically a to-do list user no more admin will be created if I don't want to right now let's uh see this lat trust admin panel so we have seted this uh we left this same route so we will be able uh to access this panel by this uh by this um route so if I check it here you go we are able to see our users we have this admin user here what we have created and if we check it as you can see we have a admin Roll But if you if you check the user number six which is created just now it's a to-do list user and you can easily uh change all the roles here now with this panel which gives you a flexible way to edit the user and if you go to the rolls page as you can see the admin is able to create read update delete user and read profiles but we haven't gave any permission to create or or delete to-do lists however the to-do list user is able to create create update delete todos but they are not able to create or delete or edit any users but at the moment if I log in to uh to the dashboard I see this to-do list even if I log out and I log in with the admin user so I'm logged in now as an admin and I still can see this to-do list menu I item however uh if you check if you check the rules it say the admin is not able to create to do and read and update and delete so basically we are not checking anything uh from the lur trust permissions at the moment so now we are logged in as admin and we are seeing this to-do list list menu item but I don't want that so now let's go back to the code and if you go down to the resources views and layouts here is the navigation. blade. PHP and here is the nav link to-do list and I want to show this only for users who are able to create to-do list so in order to do that we need to check if the current logged in user is able to create a to-do list and how to check if the user has a permission let's go to the laurat trust page of course we have an error now and if you go to the rolls and permissions check for rols and permissions and we can use for example has permission and the uh per permission name we want to check in our case if you check the roles uh let's check the permissions we want to check if the uh user is able to create a to-do so to-do create we need to do to do create has permission to do create okay now let's go back and has create and let's copy and paste this nav link inside this if statement and as you can see we are not able to see this menu item for now but if you go to this uh latast uh admin panel and if you change inside the rolls menu if you change uh the admin row if you tick this and you say okay the admin is able Now to create to-do save it you go back to the website and hit refresh here we are we are able to see now because we give them a permission we give all the you all the admin to see this uh menu item if you save it now and hit refresh okay so no admin will be able to see this menu item only just a simple user who freshly registered as a to-do list user will be see and visit this site unfortunately now we need to solve one more problem if I log in as an admin again okay so I'm of course I'm not seeing this menu item but I still can visit the route so to do so even the menu item was not there I still can see this uh route so let's go and fix it let's go to the to-do controller the easiest way is just create an if statement here check the currently logged in user is able to create a to-do list so if they have a permission to create to see the to-do list then allow them else give them a 404 error page not found that's it I have have a typo okay page not found but if we logged in as a simple user register we are able to click on it and we are able to visit this website as well okay in the next video we will see how to serve this different pages for the users and for the admins for example if you logged in as a admin you will see uh this dashboard route but you will see different uh page uh as the logged in user so you will see the same route but for the admin and for the user we will serve a different content different menu Different Page thanks for watching and stay tuned for the next video
Info
Channel: Code Fisher
Views: 2,252
Rating: undefined out of 5
Keywords: Laravel 10 roles, Laravel 10 permissions, Roles and permissions, Laravel 10 auth, Laravel 10 authentication, Laravel 10 Laratrust, Laravel 10 admin, Admin Roles
Id: WAsNLqujKHc
Channel Id: undefined
Length: 22min 58sec (1378 seconds)
Published: Sun Nov 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.