Auth Multi Roles Login with Custom Middleware in Laravel 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello my friends today video we're going to implement the custom middleware with the multiple rows in the lateral line we will have three different roles such as a user editor and and first suppose you already have a library check in your local machine and now we will modify the code in the user migration file because we want to add a new column or new field in the table users and this file is coming by default i haven't graded manually alright now we open up this file and i'm going to insert a new line which is a column row and the data type is the tiny integer because we just store the number just like a 0 1 2 which is a cell being spaced and it is represented to the each row and we have the three rows so by default i'm going to place it with the 0 which is mean the row as user so i'm going to comment on this okay now let me show you my database i have created a database given name as the laravel and in env file we have a database connection in here by default i didn't change anything and we have a database name username password so it our connection is done okay so what we are going to do is to launch the terminal in my visual studio code and we are going to run the command to migrate the table in the database well you see we have an error so let's find what's going on okay so we're going to fix this in the app service provider which is located in here and in this boot function we are going to put a schema default string with a specific lens and then we don't forget to import user class on the top here okay we're going to runs it again because we have a greater table so we have to delete the table in the database and now let's go run it again okay it's done now we're going to create a custom attribute which we will return as the value rolls in the text instead of the number 0 1 2 like that we store in the number but we can return with this value just like a user editor and admin okay anyway don't forget import the attribute and then now we're going to update the package.json because we want to build with the laravel mix i'm going to copy this and then i'm going to paste it back and now we start installing the uri and then we're going to install the bootstrap bootstrap file and with this command we'll generate an authentication page with the login and register and then we are going to install the laravel max it's a download dependencies and we're going to run the npm make sure that you have installed the node.js to run this command okay and finally we're going to run once again to compile the css and the javascript and now let's start with the dependencies bootstrap strike and the javascript okay now we done in this step we start to read the middleware with the command addition we call this a file as a user role made up here and then the file will generate automatically so let's find in the xttp middleware and there it is we are going to check the connection to allow for the next request but i will add a new parameter name row then we will paste it from the routes.php file that we're going to do next step and now we can check that if the user log in success and access the url match to their role and then be allowed to access the url ok this is not a function and for else we are going to return as the json that show the message no permission page that we can access to this page okay so the last don't forget import off and next we going to register the middleware in the canal.php i'm going to copy this and open the kernel file scroll to the last add as the next array element and copy this and paste follow the pattern and then i'm going to aliase this name to the user row paste it back here then we're going to change the class name go to copy class name and we're done registering here so now let's collab distribute a little bit what we're going to do next is to add route to match each row and the controller you can see here this route is the login and register page which comes with the authentication by default and this below same power route we're going to group the route with the middleware so let's call the middleware parenthesis and paste the array which means that we can apply more than one meter where in each round so the first we add the off middleware and then the middleware that we just graded the oauth material is the built-in middleware which you can find in here and here okay um in here we will specify row user separate by the column next we're going to group the route so if any routes inside this group have to filter with the off and user role made aware and the role must be the user we will use only one controller which is already graded and this route will call the method user home and i'm going to give it name in here we have to import where controller come from and we're going to import here which is located in app http controller and the class name as you can see here is the namespace should follow okay it's the same now i'm going to copy and paste two more for different group role and we just change a little bit i'm going to comment on this for editor route and the route as editor and put the url function name and the last one is the name of route for last one is admin url and a function name the last one is the name okay now we done so for the next we're going to create a method in this constructor we can remove this off filter because we already group filter in the route already and here i'm going to change to the user home function and return to view with the message the function in the user role i will copy two more for supply different roles editor and admin and then i'm going to change this message okay now it's fine we can use the same view but a different message now let's find a view here is a home which uses blade directive to extend from other layout that located in the directory you can see here it have a head and body of the html and if we scroll down we can see this gel keyword which is uh will embedded all the layouts extended from this layout now i will change this to show the username and the message that we passed from the controller in this step we will implement login function which is performed in a login page when we log into the specified role and redirect to the correct user role page let's say a public function login and pass the parameter request we have to import the class so i'm going to copy this file and paste it back here we're going to receive our request from the client then i'm going to validate the following email and password then we start compare checking with the database when we log in by the following code okay if the login credential is correct we going to check another row which conform with the user first is admin if the condition is matched we will redirect to the route admin home page and i will use this name so i'm going to copy it from the route.php at the same we have to check in other two rows as well check with the role editor and redirect to the home editor and for else role is we're going to redirect to the row user for last if we was incorrect plugin we're going to return to the login page again with the error message okay now let's go to database the table user idea is no any data so we're going to create a seeder to produce and sample data for testing with this command now it's gonna generate a new file which is located in database directory seeder and this file okay now we're going to implement same product user but first we're going to import the model in this class for this run function we're gonna call to generate a record i will instance three new users in different roles for testing okay the last we going to loop those array and create a user okay let's run the command oh sorry wrong command wait another mistake oh i got wrong variable which should plurals okay so let's run again hope it's worked let's see in database there are three records now let's run the lateral server homepage work okay now the page is worked properly with the style and the ui so let's key in with the first user and login oh and made another mistake in the login controller file okay this one is the function so let's do some change okay another one in the middle there so let's do it again hope it's fine now we can see the user login so i'm going to log out with another user role and see it redirect to a different route url link and if we access to the url another user which is not a match to the role that you can see it is now a permission to access alright guys hope you find this lesson helpful i do appreciate if you guys click subscribe hit the bell and turn on the notification and i will see you in the next video [Music] okay [Music]
Info
Channel: Cambo Tutorial
Views: 47,483
Rating: undefined out of 5
Keywords: Cambo Tutorial, laravel middleware, laravel tutorial, laravel 9 authentication, laravel custom authentication middleware, multi role laravel 9 authenticate, login multiple roles middleware, custom middleware multiple role, laravel roles, laravel authentication, laravel 9 auth, laravel 9 tutorial, laravel multi auth, laravel middleware authentication, laravel ui auth tutorial, laravel login tutorial, laravel auth, laravel roles and permissions tutorial, multiple authentication
Id: vc4sXOdE4bQ
Channel Id: undefined
Length: 16min 50sec (1010 seconds)
Published: Sat Jul 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.