Laravel Multi Authentication With Separate Role based Dashboards| Middleware.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone uh to another tutorial my name is William and in this tutorial today we are going to look at how to implement multi-authentication in laravel application where each user will have their own specific dashboard and then we are using middlewares and of course we will also be getting the assistance of laravel UI so here I have a fresh install of laravel the first thing I want us to do is to install a laravel UI so I'm going to open Terminal and then we are going to run the command composer require as we wait for the machine to open it we are going to run the command composer require laravel UI and as it installs it I want to pause the video such that we give it time to install we will go ahead and install the bootstrap scaffolding so I'm going to use the command PHP artisan UI bootstrap and then we will also tag oath such that it also does the authentication scaffolding for us the next thing we're going to do is to run PHP npm install you run them individually we are then going to run npm run Dev command that is successful so I would like us to go further ahead and do some more setup the first thing I want to do is to go to our EnV and then be able to set our database so I'm going to change the name of the database here and I'm going to write it as multi multi health like that and you save it the username will be root and the password will be the an empty string then I will run my zump that is Apache in MySQL and then after that I will go to Chrome and then be able to create the database which we have called multi author so you go here and write localhost localhost phpmyadmin we will go ahead and create a new database we're going to call it multi-auth multi-auth create it our database is created I would like to go back to our terminal and I would like us to run a few more commands and the first thing I would like us to do is to create to have a new terminal maybe split terminal and I can move this one almost towards the edge and then I would like to do a migration so I'm going to write but before we do migrations I think we are supposed to first of all add do some edits on uh some few things so let us first of all work on that the first thing I want us to do is to change something on the user model so you go to app HTTP app I mean app models user model and if they use a model I want to just add one more field which will be having in our database and I'm going to have the field called roll let's save it after that I want to go to database migrations then we go to the user table then we are going also to add that field so in this case probably before password I would like to have table and then we will have the values as integers so integer we are going to call it roll and in this case we are going to have several roles the first one probably we can have super admin and super admin will be one the digit one for super admin and then admin we will have a I will use the integer two then maybe Department head I will use the integer three and then stuff the integer4 and then finally client integer five so that is how our database is going to be so I want to save this and before we continue I also want to do something on um factories so you still go and the database then factories and you want to modify the user Factory so factories user Factory and here I would like to create as we are creating the the populating the database with some fake data I also want us to have another field which we have created in the in the con in the data in in our database migrations and I want to call it role and this one we are going to uh get it like this so fake and then I want it to be unique each and every value to be unique because I only want because you're having one up to five so the only thing I want are five records and in this case the records should have number one two five so in this case that is what I'm interested to do that's why I'm putting such a constraint and we are going to have number between and in this case a number between one and five that should be good enough for us foreign after that I think we can go ahead and do our migrations and then we will also do database sitting but before I do that I have to ensure that I have a created the five users so you go to database seed and here in the public function run I will even comment what we have here and so you have we are calling app models user Factory here they are creating 10 users for me I want five of them just five users with roles from one to five so in this case one for super admin two four admin 3 for a departmental head and then four for staff and five for the client so how you go ahead and run the commands for migration and the command for seeding our database so we are going to have PHP artisan migrate and that command is going to generate for us the migrations in our database or create the tables for us the tables are created after that PHP artisan DB seeds and this one will be able to populate our user table with five uh rows of data of users their emails their passwords and everything in this case the password is just a default password and then after that we can be able to continue so let us run that command also and I would like to as it runs we expect to create five users so I will go to our table called multi auth and see if we have the five users created as you can see we have five records in the user table so if we open we are going to see our five records that you have been able to use the cedar to create for us now um yes as you can see we are having roll one two three four five one two four five and then three so sort of like in a randomized way that is fine I want us to now go ahead and create middlewares and we are going to create five middlewares each for for each of the the users the user type that you're having in our database so I'm going to use the command PHP artisan Mac middleware and then the name of the middleware so the first one I want it to be super admin like that the middleware is created successfully Let Me Clear and then create the address so again we are going to I'm just going to use the the up Arrow to just return whatever is that I've typed the command that I've typed previously then here I'm going to have admin and create the admin middleware it is created I will also do the same and create department head so depth s maybe head something of that sort that one is also created are you clear again and then we are going to create another middleware which we're going to call stuff foreign and then finally the middleware that we are going to call client so we have five middlewares in total and I would like to go and open these middlewares and do some work on them and we will only do serious work on one of the middlewares and then after that we are going to just copy the data that you have in the middleware and then use it in the other middleware so you go to BP and then middleware then I am interested in beginning working on Super admin so I will open the super admin middleware and then we are going to populate it with data so it is what we are going to have of course the first thing that I would like us to do is to check if the user is authenticated so I'm going to say if the user is not authenticated so I would check foreign check and this will tell us because you're taking the negative we are checking if the user is not authenticated so here if the user is not authenticated we will redirect them to the login route so we can say return redirect and then we can just say login and we can be able to pass an error here if you are using flash messaging to be able to communicate exactly what and why that is we are redirecting them that one you can be able to do but I would just like us to continue the next thing after that the user that if the this process goes beyond here it means this user is authenticated so I would like us to just check this user and get the details so we are going to get to save in the variable user the oath user like that and then of course yes here we this something that you have left out and then of course we are going to go ahead and now check if the the user role matches what it is it is uh permissible in this uh middleware so we can go and check user roll in that case and we are saying if the user role is one this user is a super admin so we can go ahead and serve the next request so just copy this and paste inside that uh function like that obviously we are going to check because we are authenticating different users five different users we are going to check if the user has a different role so we are going to check each one of them from from 2 up until five so I'm going to check if again maybe I can just copy whatever it is that you have here the only thing that you will change is uh where we are going to redirect so here if the user role is 2. in this case if the user rule is two this user is an admin and what we would like to do is just redirect them to the admin page so return redirect foreign and then the route is admin just like that then we are going to copy this in dufo all the others so we can check for roll of three this one we are going to redirect to Department head dashboard so each user will be taken to their own dashboard and in this case we are checking whether these users are not admin users so we can be able to redirect them to whatever they're supposed to their dashboard is supposed to be then we go to staff and then after that if the the role is five we can redirect to client like that let me just use client like that so if everything is okay then we are good up here we must import auth so we can say use both like that and we will remember to also do the same in all the other controllers so I would like to I mean in in all the other middlewares so I would like to copy this everything that we're having here we're just going to paste in all the other middlewares so I would like to go to the next middleware and the next middleware is um let me go to admin such that I move systematically up here we can say use of like that and Save and then after that I want to paste all the code that we have here so on top of that and now in this case for this one we are checking for a user roll number two so the if the user rule is 2 in our database is an integer two then this user is an admin and we are going to serve them the next requests so I'm going to change this one to Super admin so we put it one and then you have here super I mean like that after that I think we are good with this uh middleware then we will go to department head this one here again up here we will use off and then after that I will replace whatever it is that we have inside here with the code that we have just copied and we are checking for a roll of three if the user has roll three in the database then it means they are uh departmental head so in this case I will go to departmental head and change it to Super admin so I'm going to just say the role is one and we are redirecting to Super admin like that and Save after that we are going to go to stuff again we are going to paste the same code we're going to paste the sample code and here the staff is a user with role number four and then I will change where we have user rule 4 here such that we can use it for the super admin again like that and then lastly you go to client also we will use oath here and I would like to go back and just check okay I will check afterwards after I have pasted this sorry after I have pasted this and then the here we are checking for user rule number five the roll number five is a client and I'm going to change this one to one for us of admin like that so I think we are okay so here we are importing auth we will I also want to check in the stuff here we forgot so we can say use oath and then for the department heads I think it's okay for the admin I think everything is okay and then for the um super admin I also find that everything is okay with that one the next thing we are going to do is to go to kennel.php inside http then cut can I so whereby we are going to as this one here if I maybe close this one you can be able to see it so HTTP then kennel this one here and then when you open it you scroll down what's up until where you find root middleware and then this is the place where we are going to put all our middlewares here the first Midway super admin we will name it that foreign we are going to say up just going to say where it is located app http middleware and then super admin class like that and then I will copy Ctrl alt down in Windows the and then we have admin and then here I will change this to admin will you still copy that line con shift alt the down arrow and then here we are going to have the department head thank you and uh the middleware is Department head exactly the way it is there then we are going to to also have we are going to also have stuff and the middleware is going to be stuff like that then we will still have the last one which is client the middleware is client like that after that we are going to do some few changes in our controllers both controller the login controller and here I would like us to just modify and do a few things so inside this login controller I want us to sort of modify and I want to as to create a function here which you'll be able to attend to our login so I'm going to say projected function login after that I would like us to validate so first things you're going to do is to have request and then after that we also ensure that we import the same above here foreign use a new minute HTTP request like that then now we can be able to do a little bit more the first thing I want us to do is to validate uh the values that are coming from our login form so in this case credentials we are going to say request validate and we are going to say the email because I use how you login using email and password so email the email is required and we want it to be an email and then after that password and for the password just want it to be required like that after that we are going to log in our user and so we are going to use an IF function to and say if both attempt if auth attempt and we are passing in the credentials these ones that we have in here then this function will attempt to log in our user so if the user is not logged in we can say else and we return them to the login page so in this case we can say return redirect and we can just say login again if you want to pass a flash message you can be able to do that one there but I am not very much interested because I want to make the video as small as possible you can find out what you can be able to do in case of a problem you can hit me up in the comments we can see how you can be able to assist each other then we are going to I want to get the role of the user so I'm going to say user and this is the logged in user so we are going to say oath user and then what I'm interested in is just their role and since we are using the helper oauth everywhere I would also like us to just import it here so we can say use of like that and then um so we are saying we want to get the user is authenticated and we want to get their role this role is what we will use to also ensure that they are redirected to wherever they are supposed to the various dashboards that one is supposed to be redirected depending on the on their role so uh we are going to use a switch statement I wanted to use um an IF function and I've seen it to be somehow verbose so let me use this one so we are going to check the user role and uh after that we are checking for several roles so the first one is When the Roll is one and what I want us to do is uh we redirect return redirect so I use redirects uh so I use that with the with a roll of one is a super admin according to what we have done we have a set in our database then we are going to break such that the function you know to continue then we will go to case number two so I think I'll just copy this one and paste for case number two the kiss were the user is a let me return this one a little bit just a little bit of formatting such that it's a little bit more readable so here we are going to admin and in this case this user has a role of two after that we are going to check for a user with a roll of three so for a user with a roll of three this one we are going to redirect to Department head after that we are going to check for a user with a roll over four with the role of four the user with a roll of four will be redirected to staff and then finally a user with a roll of five so in this case we have a user who is a client that is what I have a called this user then for defaults I do not know what we are going to do about the default value but for me I so for this one we are redirecting to client so for default I don't know what I should do but uh let me just log this user out because if somebody is not a department is not a client or staff or departmental head and or admin or super admin and our table only caters for these ones then it means we will have to find a way in which we will deal with this but I think I want to just log log this person out so oath log out and then I will redirect to login so return redirect to login and I can pass some message so with that you can say error this is what you're supposed to do with the others if you have set flash messaging in your front end and here I can just write oops something went wrong something of that sort and then I think we we are done with this one if everything is fine so I will save what we have here I think we have used several one two three four five I think here I have a problem I'm referencing user Rule and I'm having here user so supposed to be wrong so like that so user roll if that one is okay this one and this one should match because it's the same thing I hope I've not made some error somewhere which is similar to that because if you break our application anyway we I want to go to routesweb.php and be able to create routes that you direct each and every user to their specific dashboard and here we are going to have Roots Gates and the first one that you're beginning with is super admin and I'm going to have a function since we are not loading it via controller via controller therefore we can just do like this and here we can return View and I just want to call the view super admin like that of course we are supposed to chain the middleware so here I'm going to say um you can say the name of this route you can make it a named route super admin and then we can also chain the middleware so maybe where in this case the middleware is super admin like that so uh then I'm going to copy this four and use it for all the others all the other routes and change just a little bit of whatever it is that you're having so the next user is admin and I will change everything to admin here the middleware is a mean everything here is admin like that then I'm going to again copy and paste so here we are going to have a route which is a department head so depths head like that and I just want to again copy use one and paste everywhere even the middleware is the same we are going to copy this again and we are going to paste it here this one is now we are going to have what we are going to have stuff so you also copy this and paste everywhere where we are having replacing whatever it is that we have for the department head everywhere like that way so we have stuff then of course finally the last route which is going to be the client I'm going to copy and pastel through again and save like that so I guess we are almost there I want to go and create several dashboards so you go to resources views and I'm just going to copy the home blade so I'm going to create the several views that we we need first view I will call it super admin dot bled dot PHP the next view I'm going to call it admin dot bled dot PHP and then I'm going to have another one I'm going to call department head so depth heads dot bled dot PHP then I'm also going to have another view which is going to be called stuff so stuff dot blade dot PHP and then finally the last one is client dot blade dot PHP like that so after that I'm going to copy whatever it is that we have in the home blade and then we pissed and do some small modification so I will begin with super admin I copy this and begin with super admin so in super admin I'm going to paste and uh I'm going to change here where we have dashboard I'm going to call it super admin dashboard super admin I'm calling it I mean Super admin like that and then where we have you are logged in we can say you are logged in as super admin like that let it just be like that then I'm going to copy everything that we have in here and paste in a in the admin model I mean in the admin view so here where we are having super admin I will just delete the word super and also here and Save then we will go to department head I also paste the same code and we are going to have a let me change this to depth head if like that and then also here department head like that then we are going to stuff also in the stuff blade we are going to just erase this one and write stuff and maybe here also stuff like that then finally for client I'm also going to do the same I'm going to paste then we are going instead of super admin we are going to replace this with client and here client so our work is finished I would like to run our laravel application and see if we can be able to do to attempt and see if we can be able to log in so PHP uh tson serve I would like to pause the video a bit our server is running so I can be able to visit it on a on Chrome and I will type localhost 8000 and obviously we have the authentication routes but for this register you will disable it because again under the circumstances somebody cannot be able to register and give themselves a role and even if that is possible it will be a very funny implementation so this one you can disable it in the routes for web.php so let us attempt to login I would like to copy a user here this user is a super admin and I will copy their email and attempt to log in and see what happens it's still opening the login route maybe I can pause a bit so we are going to paste in our user here and the password is just password and attempt to log in my machine is a little bit slow and as you can see we are logged in in the super admin dashboard so it says super admin dashboard you are logged in a sub admin I would like to log out and try logging in as a admin so it's logged out login again I pass the email here the password is just password the word password and that is what is set in our factories so if you check the default is just password even this one when we check it says admin dashboard you are logged in as admin I want to log out and try logging in as a department head to just check if the code is entirely working perfectly so are we have logged out let me log in and we also have password as the password small letters again let us check and see very nice we are logged in as the department head and as you can see each person is being directed to their own dashboard so if there are further things that you're supposed to do for your implementation you can be able to just work in a way uh in which these concerns are separated so then we have um I also want to try to log in as um stuff so you log out in again of course the password is just the word password again we allow dinner stuff and finally I want us to attempt to login as a client so I will log out this stuff I will log in now as stuff paste that one there enter the password login we are logged in as client so as you can see our code has worked very well thank you very much for following this tutorial up until this point I ask that you may subscribe like comment such that we can be able to be encouraged to create more useful videos thank you very much
Info
Channel: LaraPhant
Views: 17,653
Rating: undefined out of 5
Keywords: Larave Multiauth, Laravel ui, laravel seperate admin dashboards, multiauth using middlewares
Id: BXl5mOle0z0
Channel Id: undefined
Length: 48min 5sec (2885 seconds)
Published: Wed Feb 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.