Laravel 11 Breeze Multi Authentication tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to this tutorial my name is William in this video episode we are going to look at how we can Implement multi- authentication using Breeze liveo inside laravel 11 laravel 11 has just been released uh recently and as you can see the look and feel is very good but I want us to go ahead and see how we can be able to use Breeze in this um application and also be able to implement multi- authentication so what I want us to do first is to install a breeze um package so in this case we will do PHP it is an I mean composer require composer require LEL for/ PR and this command will install for us Breeze but of course including every other dependency after installing Breeze then we are going to run the command PHP Artis Breeze install let me just resize a bit and I want us to choose the stack as um Live Wire High functional dark mode yes PHP test we can use pest or PHP unit I will not be doing testing in this video this of of course you install liveware and volt on top of the breeze that we have already installed our Breeze live application is fully installed and if we are to just check here we have now authentication if we to refresh we have login and register I want us to go ahead and do a modification in um the SQL database that we have here so in the migrations the first migration is the migration that has everything in L 11 you can see we have all the migration regarding the user in one yes and so what I want to do is to add one more row which I'm going to call sortable integer I'm going to call it roll and in this case I want it to have a default value and the default value is three so we are going to have several roles we will have a roll with um a roll one it to be super admin and then role two will be admin and then a ro of three will be the normal user like that so basically that is what we are going to have and so I would like to do my migration again since the migration was done with the installation of LEL so this case I'm going to do PHP Artisan migrate fresh and it will run our migrations again now I want us to go ahead and register a new user just one user whom we will use for the testing of our application so the name is uh William and uh the email is William example.com then password and after doing that we are logged in I now want us to go ahead and make Provisions for multi- authentication for the three dashboards that we require and so inside our terminal I would like to create views so in this case we can have PHP artisan make view the first first view I want to call it admin and then I want to have another view which I'm going to call Super admin and then I use scroll to where those views are inside resources views and then uh go to dashboard. Blade and copy whatever is that we have here that is what we are going to use in the new newly created dashboards so in this case we can paste this here and then because this is the admin dashboard we can call it admin like that and save and then we will also paste the same in the super admin dashboard so again we will paste that and then above here we will call it super admin dashboard after that we need to also have the routes that um will be able to lead us to these dashboards so so I open route web.php and I would just like to copy whatever it is that we have here and then we are going to paste it down here twice so one for the super admin and the other one for the admin and so this one I'm going to call it admin and we have admin cuz the view and even the route I want it to be admin after that I also want us to have this one as super admin and uh these one as a super admin also the name super admin of course if we are to log in now we will be actually directed to whichever dashboard that we have and since all of them you have to have the authenticated user but in this case one can be able to move from Super admin to admin by just changing the URL and that is not what we want we want to be a little bit controlled in whatever it is that we are doing so in this case I would want us to create uh middlewares so we are going to create uh several middlewares actually three of them PHP Artisan Mech middleware and the first middleware I want us to create is called super admin I also want us to create another middleware that we will call admin lastly I want us to create a middleware that I'm going to call call normal this case to stand for a normal user after that I would like to go to the Super admin middleware so you go to app HTTP middleware and I want us to begin with super admin and here what I want to do is to uh have an IF function here and since we will be accessing the authenticated user we can say use eliminate support facets out so use eliminate support facets out like that and inside here we can be able now to get access to our user the first thing that I want us to do is to check whether the user is authenticated so we can just check if our user is not authenticated if oath we can just say oath check instead and then if that is the case if the user is not authentic ated we can uh return redirect and we are going to redirect to Route so return redirect route login but if that is not the case and the user is authenticated then we can be able to get the user R so we can say user Ro is equals to out out user um R now we can have if uh user Ro is uh equals to one so if the user R is equals to one and this is um the super admin class or in this case the super admin middleware then we can be able to pass in the next request and allow the these user to access the resource so I'm going to just cut whatever is that we have there in past here and then we can be able to check for the other options so if user R is uh equals to two then this one is an admin and this one we are going to redirect them to a route so let me just copy what we have here we copy and we are going to redirect them to the admin route so here just going to say admin now we copy one more time and then uh paste here and then we check if the user Ro is three and we are going to redirect to dashboard that is now the normal dashboard that we have in um our uh class in in our Breeze application basically so basically that is how the this middleware will look and I would want to have the same uh thing coped to the other middleware so I want us to go to admin middleware and I want us to paste replace everything that we have here and so here I'm going to change this to two so if the role is two we will pass in the next request if not we will go to super admin so in this case if the role is uh one like that and then every other thing is okay then we go to the last one which is for the normal user and here we can also replace the same and then now for the next request it has to be user number of user R three and uh then this one here I will change to one and for this one we are going to go to super admin route like that so basically that is how our middleware will look of course above here we will have to import the O facade so use illuminate um support facades and then out that way and also I think there is also one more place inside the admin one we have not been able to do that so even here I want to pass it in I now want us to go to in LEL 10 we used to have a file that was called kenel inside the HTTP folder but now it is not there so in LEL 11 we go to a folder that is called config and we go to a file that is is called up this is a place where we can be able to declare our middlewares and so what I'm going to do is to then uh import our middleware to this place so in this case above here we can say use up HTTP middleware and then we have super admin like that and then I want us to just copy these on separately the next middleware that we have is called admin and then the next middleware that we have is called normal and then I want us to use the middleware here with aliases so we are going to if we want to use them with aliases we will pass them inside this closure where we have the middleware so here I we have middleware and then we can say uh Alas and then we can have the brackets and square brackets and you close here and then we can have all the middlewares that have aliases so these ones I want us one of them to be called super admin and this one we can say super admin class the next middleware is called admin and this one we are going to access it with admin class and lastly we have another middle this one I think it should be let me change it to small a then this one should be normal and I want to have it as um normal uh class like that so basically we've been able to create our middle and now I want us to use them in our routes so we will go back to our routing file in web.php and here on top of the midw that we have so for for dashboard we want the dashboard to be accessed only by the normal user so I add here the normal mware and then for the admin we will also among the mware groups we will add admin and then for this one for super admin we also have a middleware that is called super admin so basically that is that we just have one more thing to do and then we will be set so I want us to go back to resources and then inside here I want us to go to a folder called Livewire and inside Live Wire I want us to go to Pages inside here we have the login. blade so I want us to differentiate the users when they login when a user registers themselves in the system they'll be considered a normal user but then this um privileges can be elevated and once the value is changed from 3 maybe to two to or to one for the role then they can be able to have access to the other dashboards so let me just um uh show you where we are going to do something so as you can see when the user logs in in inside this login function or method we are authenticating and then we are regenerating the session and then we are redirecting so here where we are redirecting is where I want us to do um redirection to several places not just to this one place so the first thing that I want us to do is to get the user Ro and the user Ro is given by out user and then uh roll uh that way after that I would like to use a switch statement to enable us to be able to redirect each and every person to wherever it is they are supposed to go and so I'm going to have switch I do not want yes I do not want uh it to generate for me all those things at once so user Ro so we are passing in the user Ro here now we are having the several roles so the first r that we have is a one so if we have a roll of one then I want us to do a redirection and I'm just going to use whatever is that we have here so I just cut and uh paste here and then we will have break like that but now this one we are redirecting to super if the role is one and then if the role is two we are redirecting to admin and then lastly um if uh so again we can break and then lastly if the RO is uh three we can redirect this one to dashboard if the RO is three so after that I would like us to again break and then lastly whatever I tyed break lastly we can have default and for the default we can just say return redirect and we can uh just send them anywhere maybe to the login page or we can just send them away completely from take them to the homepage of our application so basically this one should uh look like that let me just extend such that you're able to see everything that we have here so basically that is what we have I want us to go ahead and attempt because already we are logged in with a user who is called William and their role is number three I want to change the role of uh this user and what I'm going to do I'm going to use uh Tinker to change the roles so in this case we can say PHP sorry down here PHP Artis tinker and here we can say user is given by app models user find we only have one user in our database and that user of course you have an ID of one so we can find that user and we have them and I want us to just change the rules so we can say user Ro is equals to one and save and now it is saved and then we can say user uh save like that and close with a semicolon and the user is saved because it has returned true I just want us to refresh and see what happens and now you can see our user has been changed to a super admin I still want to try it one more time and change the user R again so we can say user uh role is a two and then we can say user uh save again the user has been saved so if we are to refresh one more time now you can see that our user is an admin and if we are to log out this user and attempt to log them in again they will log in as an admin so let me just attempt to do that so I will put uh William example.com and then the password if I attempt to log in the user let us see what kind of dashboard you're going to be sent to and we are sent to the correct dashboard which is the admin dashboard thank you very much for following this tutorial up until this point if you have not subscribed to this Channel please consider subscribing and if you like the video give it a thumbs up and you can also comment in the comment section if you have an issue a query a question or maybe a contribution that can be able to make uh this project better thank you very very much
Info
Channel: LaraPhant
Views: 6,726
Rating: undefined out of 5
Keywords: laravel, laravel 11, multi authentication, laravel breeze multiauthentication, livewire breeze multiauthentication
Id: gm5oYOZGN00
Channel Id: undefined
Length: 18min 35sec (1115 seconds)
Published: Wed Mar 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.