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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back to the channel today i would like to show you how to create multi-out multi authentication uh in laravel 8 which would work with laravel breeze laravel jet stream or laravel uh fortified as well so basically you would have users with different roles in your application one user is an admin role and the second user for example a student or teacher or blog writer so you can give different roles for different users in your applications and they can visit different pages different views in your application okay so basically in my case i created a free type of users admin blog writer and a simple user so if we are logging as an admin okay so now we are logging and we are seeing the dashboard for admin and you are logged in as an admin we don't have any menu item because i haven't created you could create as many menu items for admins as you want and you can show them whatever you want here nobody else could see this dashboard except users withdrawal with admins and as you can see we are on a dashboard route if we are log out and create let's create a new user i use fake filler here and let's create just a blog writer for now as you can see we are see uh again on the dashboard route but we can see the dashboard for blog writer and you are logged in as a blog writer and we have a new menu item here create a new blog post for example and we can visit this dashboard slash post create route and here would be of course some type of form to create a new blog post but i haven't created for this tutorial now let's create just a simple user okay now we want to create a simple user and as you can see we are again on the dashboard page dashboard route but we see different views now we see the views for users so this is the dashboard for user and we can see a different menu item here we are not able to see the create a new blog post but we have my profile for example and we can see our name and our email address and we are not able to visit the post create route so post create 403 so we don't have any access to visit this page and you can customize any error message you wish okay so that's it uh first of all please hit the like button if you like this content and you find it useful and subscribe to the channel if you haven't done it yet and let's get started so first of all let's create a new laravel application i will create a laravel breeze application but as i just mentioned it would work with the laravel jet stream or laravel fortify as well okay so let's go to the command line and i use a lot of new and my application name it will be out and while it's installing our new application let's create a database for this project so i'm going to sql mysql and hit new and i'm gonna name exactly the same as i named my uh laravel uh project so multi-out and i select m utf-8mb for unicode ci it creates so our database is ready right so our new application has been created now let's see the inside into its folder so cd multi-out and now we inside this newly created project and let's continue with php artisan migrate command paste right migration is done and now let's require our laravel breeze so composer require laravel breeze dash dash dev okay now we need to install laravel breeze that's it as well now we need to do the npm install and npn run dev commands so just copy this paste down here right that's done as well now let's run our server so php artisan serve now we are now we can open our project on this address here you go so we have a freshly installed laravel breeze application ready to go let's open it in our vs code right so i open my project in wes code and our server is running as well so let's open up one more command line because we need to use another command line so as i mentioned we will use lara trust is a role based access control package for laravel it's very useful if you would like to create a multi-authenticated laravel applications so basically you will have users with different roles in your applications so let's say you will have a user with admin permissions the other user will have for example only teachers permissions and they can access different pages on your website and they have different rights in your applications okay so it's a very useful package for multi-out laravel applications and as you can see it's a compatible with laravel 8 as well so let's click on get started and here you have the installation let's do the very first command composer require lara trust so let's go to this command line paste it here right next we need to publish our trust files as well right it's published now okay next is a php artisan laura trust setup type in yes lesson as well okay and just before we would run the migration let's go to the usage and cedar and we would like to run the php artisan la trust seeder as well right let's publish this seeder files okay let's do the composerdump autoload as well and now we need to copy and paste this command into the database seed folder and database seeder.php file so let's copy this command and go to vs code and we have to open the database folder seeders and databases let's delete this line from here and paste our code as well let's save it we can close this up okay and just before we would run the migration and the seeder i would like to show to you how you can set up your different roles in your application with the seeder so if you go to the config folder and if you go to the laura tracidor as you can see by default you would have four different uh user roles user types so you would have super administrator administrator user and this would be for you so you could customize it as well but you could customize any of these so for example you could have admin in your application and the sec and you could have just a simple user so you would be the admin and you would have a user in your application or you could have teacher and student roles so you could give them different access to your applications so you have to set up everything here if you want to so let's say you would have a blog posting website you would have an admin like you would be the admin and you would have blog writers let's say so let's say you would have blog writer role and you would had you would have simple users who were reading your blog post okay so in this application i will have an admin a blog writer and a simple user with different uh different permissions in my application so let's jump back to our website our lara trust website and go back to the installation and the last part was the php artisan migrate now let's do the migration right so we have created the lot of trust tables in our database let's have a look if we hit refresh and we select our multi-out table as you can see we have user rows and permissions as well so you you could see all the rare trust tables here but all of them is empty at the moment but i want to see the admin blog writer and the user so i see them by default in order to do that we need to use php artisan db seed command and as you can see we have seeded our database let his refresh okay and if we are going to the roles you can see we have admin blog writer and user and we also have a display name of them so this is basically for the application the id and the name and this is for to display if you have for example a a control panel or something like that okay so basically we have the three different type of rows and by default now let's do every user who register on our website we want to make them as a simple user so no permissions to any other pages and no basically no extra permissions just a simple user in order to do that we need to assign a role to every user who register on our website if you go back to the largest home page you will see roles and permissions and if you scroll down you will see user user roles assignments and removals and you have to select the assignment so basically this is the only piece of code we need for now let's copy this code and go back to vs code and if we are going to the app folder http and controllers out and the register user controller if we are scrolling down this is very validate the new users uh data and that's where we create a new user in our application so just under this uh user creation we paste our code from lara trust website so basically we create a user and we attach a role to this new user and you can attach a role to them by typing what type of role you would like to assign them so you could use user or you could use the id of the role in the database so if you go to the database you can see the name is user so as you can see this is different from the display name we have to use this user or you can use the id number three or if you want a blog writer by default uh every user who registers on your website then you need to then you need to type in blog writer here or the blog writer id which is number two but at the moment i want every user registered on our website just a simple user so i type in user but as i just mentioned i could type in number three here as well it would work okay so now if we create a new user loratrust will automatically assign a role for them and they will be user in our application just a simple user let's hit refresh and if we are going to register i will use fake filler hit register okay so we are signed in and if we are check our database we should see our first user with a role id number three so if you are go to the raw underscore user table as you can see we have the very first user with a role id number three so basically they are a simple user okay so let's log out now and let's take it one step further let's say you want the registration page uh a bit more complicated so we will have a select option here uh the user who register on our website could select if they want to be a blog writer or just a simple user so let's create this drop-down select as i already created this project for a preparation i just copy and paste the code and i explain it what we are doing okay so let's go back to we as code and let's go for the registration form which is inside the resources folder views and out and register.bay.php and if you scroll down you will see the name email address password just we have here so under the password confirmation we will create a select drop down password confirmation and just under this line we will created this select option roll type so we would have a label register as and we would have a name of this field role id and we have two options one of them is a user with a value of user and the second one is a blog writer with a value of blog writer so these values are coming from our database basically if you go to the rows this is what we can give in or i could give the id of the role as well but i just typed in user and blog writer as you can see user and blog writer okay so now we have the drop down select option if we go back to our application and hit refresh as you can see in our application form we have the user and the blog writer now okay so in order to save this and assign a user to the selected type of raw we need to save it as well so let's go back to the vs code so as you can see this select option has a name role id so basically we need to catch this role id and we have to save it in a database so let's go back for the registration controller which is inside the app folder http controllers authentication and registered user controller and instead of hard coding the the default type of the user role we want to catch that request role id which is coming from our form okay so basically we attach a role to the user based on this based on their selection only on the form hit refresh use the fake filler so our first our our second user will be a blog writer hit register okay we logged in let's check our database so the blog writer id is number two so let's check raw users our second user is a blog writer has a blog writer role okay let's test this out with a simple user again quickly register fake filler instead of broke writer we want the user now just a simple user let's check our database and as you can see our third user is a simple user again just like our first user we have created okay now let's take it one step further and we will use the same route for every user when they log in but we will show the different page with different menu items for them so how to do that we need to to create a route and the dashboard controller for this purpose again as i mentioned i already created this project so i will copy and paste so i speed up this tutorial i don't want to waste your time so we have to go to the routes folder and the web.php and this is our dashboard route at the moment but we will switch it okay so instead of this we will use this route so we will group a a route so we will have a middleware so the user has to be authenticated this is the only thing we need to take care so the user has to be authenticated so if the user is a guest user they cannot visit the dashboard page and every user who register or login will be redirected to the dashboard dashboard route and we will use the dashboard controller index method to show them a a view so let's create this dashboard controller with this index method so let's go to so let's go to our command line again and use php artisan make controller dashboard controller okay hit enter and our controller is ready so we have this dashboard controller now in our app http controllers folder and we need to create this index method so let's go for the dashboard controller which is inside the app http controllers and as you can see we have this dashboard controller and now let's create that index method so public function index okay so now we would like to decide if the user is just a simple user or a blog writer because we want to show them a different page we want to show a different page for a blog writer and the different page for the simple user okay so we need an if statement here and we have to check the authenticated user role what what is the authenticated user role okay and we will use a lot of trust uh helper here that's why it's so easy to implement with a lot of trust these things if you go to the usage and checking for roles and permissions if you hit click on it as you can see the very first we can check the user has a role and you have to type in the role type so in my case will be blog writer and user or it could be admin in my case so we need this has role and what we want to check and it will give us a force or true value back okay so let's go back to the vs code and check and use this helper has raw and let's say if the user is a simple user then we want to show them a a view if let's check again our authenticated user if a blog writer at this time so if they are block writer we want to return them a view we haven't created any view for them just yet and let's see if our user is for example an admin so let's create one more else if and let's copy and paste all this code again and we want to check if they are admin and we want to show them for example we would like to show them for for every admin we would like to show the dashboard page or dashboard view for users you would like to show them user dashboard and for brook writer we would like to show them the blog writer dash okay so we already have the dashboard view if you go to the resources folder and views we already have the dashboard so let's copy and create a new view for users so the user will see the user dashboard so let's create that view okay we would like to make them inside the views folder and we will call it user dash dot blade dot php and i paste the dashboard code here and let's create one more view for our blog writer dash blog writers it will be called blog writer dash okay so if we are going to use new file dot blade dot php and paste again okay so it will be dashboard4 blog writer you are logged in as a blog writer dashboard for admin and you are logged in as an admin and dashboard4 user and you are logged in as a user okay so we have different views for different users we have views for admins blog writers and users we have created the controller to check them what they are exactly and we have to import the authenticated uh class so let's do that so make sure you you would you would have the same align of code in your file as well so we can use this class okay everything is saved let's try this out we have to log out and register a new user use the fake filler okay now we will create a blog writer so we will we should see a blog writer dashboard register okay and i have a typo i think i have another typo here as well hit refresh and yeah we are seeing the dashboard for for blog writers so basically we are not able to visit our users dashboard at all because we are using the same route but we are showing a different view for different uh different users so let's log out and hit register fake filler and now let's create a user and we are seeing the dashboard for user and we are logged in as a user now let's create a admin for example in order to do that we're just using a hard coded way basically so if you are going to the register controller yeah we hard coding we want to be create one admin so you can use number one or you can type in admin but not both right and let's just create one admin user as well register and it doesn't matter what we are selecting because we hardcoded that it has to be an admin so hit register and dashboard yeah i have a typo again what a surprise we missing a d dashboard it's refresh it should be logged in as an administrator you are logged in as an admin so basically we are seeing the same route with different view let's log out now and let's uh turn back because by default we don't want the user to be registered as an admin so we'll use their selections from the registration form okay so we have now we have now one admin and that's it the other users who are registering they have to select either the user or the blog writer role right but now i would like to show the user and the blog writer different menu item on the top so let's just create one more user so for example for the user i would like to show a menu item here up here my profile so let's just do that in order to do we need to find this menu in our application which is inside the resources views and layouts and navigation and you will see the dashboard here which is this menu item so now let's create one more and let's say we want a dashboard slash my profile same here and let's name it my profile okay so of course we don't have this route yet so probably it would be through an error for us exactly our route is doesn't exist yet so we we have to create this route but we want to specify that this route is only viewable and visitable for only rows with users okay so let's go to our route folder and web.php and let's create one more route for users profile so these routes will be only four users so let's say if you have different pages for users so edit profile show show my profile or stuff like that you don't have to group them again the route automatically will be checked if the user is authenticated and has a role called user okay so the route will be dashboard slash my profile name my profile okay so basically inside the dashboard controller we need to create one more method called my profile but it will be only visible for for users okay so public function my profile and we want to return a view for them and let's call it my my profile view so we need to create this view so let's go to the resources views folder and let's create this blade file okay just for simplicity uh users users dashboard let's copy this user dashboard and let's paste it here so dashboard for user and you are logged in as a user and let's say we want to show them their name or their email address and stuff like that so we can use the authenticated user here again name and let's say you want to show them their email address as well same again but at this time we want to show them their email okay so this is only for users hit refresh now okay so we can see my profile we are on the dashboard page for users and if you click on my profile as you can see we are still logged in as a user and we can see our name and our email address as well okay but we have one problem here if we are look out and we create a blog writer now as you can see we are logged in as a blog writer but we can see the user's menu item here and but if we click on it we don't have any access to check that page because this page only visible for authenticated user who has a role id or role user so we get this 403 message you behave don't we don't have the necessary rights to visit that page but of course we don't want to show this menu item for blog writers or for admin because we are not able to visit them anymore so there is no point to show them so let's go back to our vs code and if we are going to the resources views layouts and navigation.blade.php let's create an if statement here okay and we have to check if the authenticated user sorry and again we will use the trust helper here has raw and you can specify the let's specify the use for user x okay so this will be only visible for only users let's try this as you can see it's disappeared already what if we are lod blog writers as you can see we are able to see the link again we are of course not authorized to check that page but we can see this link so let's go back and set it to user and let's create one more if statement for blog writers so create a new blog post let's say and it's only visible for blog writers and let's do dashboard post create so under the dashboard slash post create we want to show them a form to create a new blog post and it's only visible this menu item for book writers let's do the route for that okay copy this paste here for blog writers so the user has to be authenticated and the role has to be blog writer okay and the route will be post create um okay and post create again right now let's create this method inside the dashboard controller so post create let's go to the dashboard controller one more public function post create and we want to return a view for them let's call this post create and let's create this view post create inside the resources views folder pause create.blade.php hit enter and let's just copy and paste from the blog writer where is this blog writer dashboard to the post create view and okay so of course there will there would be some some type of form to create a new blog post okay so hit refresh we can see the create a new blog post now and we can visit this page so it would be a form of course and of course if we try to visit the my profile for users we have not able to visit that page because we don't have the access and you can customize this message if you go to the config folder and lauratras.php scroll down and here is the error message if we don't have our necessary rights so you can customize it so you can customize it if you want to hit refresh and we have the new error message now okay so that's it for today please like this video if you liked it and please subscribe thanks for watching bye
Info
Channel: Code Fisher
Views: 43,685
Rating: 4.9363184 out of 5
Keywords:
Id: VHhmfjhu_1g
Channel Id: undefined
Length: 40min 32sec (2432 seconds)
Published: Fri Feb 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.