Multi login system using middleware | User Login | Admin Login | Laravel 9 | Learning Points

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome back to Loyal 9 tutorial in this video we are going to create a multi login system right so we will have a normal U type of user will have admin type of user how we can log in and how we can separate their dashboard right so if you just go to the login I already have a user at the rate email.com and this is the password and if you want to login you will get welcome to user dashboard and this is the URL and if you just are going to access the admin login in admin at the rate email.com and the password I just provided and you can see welcome to admin dashboard right and another thing if you want to restrict your url suppose if you want to login as a admin sorry as a user and if you want to access the URL from admin then what happens so simply write admin slash home then what happened you have no admin access so this is the complete multilogin system tutorial and you will see all the Restriction with the mid layers so please see the full video hope you understand all the things step by step and please comment when you got any problem or if you have any confusion I am going to start this application from very scratch so we'll create this project now and I will do all the things step by step now right so please see the full video and please like And subscribe this Channel and share with your friends if you like that video so first I just going to create the project suppose I am using composer to create a new project so simply write composer create a project and laravel Slash learnable and suppose my application name is multi uh login system okay so this is my application and it's just installing and you can see the version is 9.5.2 this is the latest version for right now and the application is successfully installed right so now just go inside the application and just go to the CD multi login system and just open the visual studio your code I will do all the things into the visual studio code and here the application is opening now just run this application and let's see PHP Artisan serve okay and what is the problem Artisan sorry artisans so application is started into this link so just go and let's see 127 and here you can see the reliable version and my Phu version that is 8.0.22 okay so this is the application started now here I just going to add the authentication like things suppose you will now I just going to add authentication system like here you will have the login and registration link I will not do all the things manually I will use the larabelle UI authentication part right so using UI authentication how we can do this and manually if you just want to create any custom authentication I already published that video you can check my playlist right now I just going to install the allowable ey authentication just open another Tab and here my application is running and all the things I will do in this tab right so simply just use composer and require to install any application composer require larabelle slash UI okay so it's installing into your application that is the UI version is 4.2.0 okay and I just going to use the bootstrap authentication so simply write PHP artisan Artis sa and Artisan UI and here write bootstrap and we'll use the authentication auth short form right and let's enter and it's added authentication scaffolding that is successfully and boost up scaffolding installs successfully right so please run npm install and npm run-depth to compile your fresh scrap holding so if you check now you will not see any things right so you will not get the controller models views right so when you just run the npm installs actually it's needs npm so here if you check my npm is already installed or not so NPA minus P already I have installed npm 8.1 1.0 still I am installing so if any things if need it will install automatically npm install and then I will run the npm Run depth sometimes it may take some time right so stay tuned okay npm installed and now npm run Dev okay so it will compile and it will create these things right so okay and now just add the queue to complete and finally npm run build okay so it will build the application so it's actually billing for a step folding actually right so it will create the build right so now you can see the public built manifest.json and public build assets all the things you will get and here if you check you will get some things there is a scaffolding part inside your HTTP you will get this auth folder all right so inside the auth folder you have this type of things that is confirm password forget password login controller restart all of the controllers things here and also you will get some view file also inside the resource inside the views you will get the auth view file and here you can see the login register View files and this password things right so all the related views files are here and another thing if you check inside the routes you will get inside the web.php you can see these things are added okay and also these things also added right so this part of the things are added automatically right now the thing is uh here I just going to add a two types of user that is the user normal user and admin type of user okay so now I just going to connect this application with the database right so first I just going to check what my current application looks like so if you refresh you will get these things login and registration actually I use the laravel UI authentication with bootstrap authentication right so simply just click on the login you will get a complete login page where you will see the email address password section so which actually need for login and there is a forget your password option so there is the email address the same password reset link means actually we need to connect with any email server SMTP server so I will again do that later and here this is the registration part so as if you are new you can register your own and you can log in anyway right so also you can understand now uh where to register we need database so just go to the localhost PHP my admin and here I just this is my password already provided to the same things now just go and create a new database suppose my database is multi-user login right so multi uh user login this is my database just copy their things and I need to configure this into my application so just go here and inside the dot EnV file you just go to the here and DB database is multi-user login the username is root and here is my password ABCD one two three Android okay so this part are done now we can migrate but will not do why because I need one things right so if you are a normal user and if your admin user how we can we distinguish so for now just go to the migration file let's check what is there so just go to the database and inside the migration ah if you check this is the create user migration right and here we have a name email email verification password all the things which we need there is appear but I need another column I need to add one extra column that is I want to check whether the user is a normal user or admin right so I just going to add one extra column that is easy admin so if the user is admin type of user then it will ah save as one or something so I will treat that if is admin then it will save one by default I will use the zero so simply just you can add the default zero that means if your node admin user you are normal user that's it simple now you can mic it so if you migrate then this four table will generate along with the new migration table right so just go here and write PHP Artisan migrate okay so it's migrating very simple now just go into the structure and you can see this portable and a new migration table actually this will take all the records which actually migrate right and the thing is which I just change if you just go to the user table you can see this new field added and the default is zero right so actually I don't need this better actually I want to change this right actually haircut take lots of space so I just going to drop this table and I want to remigate the user table so as we have the records that is the user table already migrated so if I ah just going to migrate again it will not migrate so that reason I just need to delete that things OK this is my fault I just going to migrate this should not be string actually right at least not should be string so here I just write a Boolean here b double o l e a n okay so this is a Boolean that means I will use only one R jio why we will use string right so simply now just migrate and it's migrating now if you check the database so here you can see the users table and just go to the structure and you can see the tinant one okay so only one or zero will save okay so all are done right now another thing we need to do inside the model I need to configure something so just go into the models and we will use the user model to access this user's table so here you can check so this is the fillable property that is name email password I want is admin also into this fillable right okay so model part is done and my migration is done okay now the thing is I need to create a midi layer okay I will do the middleware part later I just going to uh use the existing things suppose I just going to register something so here I will register two things one is admin type another is user type so simply just write user or simply write normal user and user at the rate email.com and password one two three four five six seven eight again one two three four five six seven eight and register okay so you are logged in so if you check here so you can see a normal user and is admin is zero okay and you are logged in and now if I just going to create another user so like this is the admin user okay and here write admin at the rate email.com one two three four five six seven eight one two three four five six seven okay actually and add any drop down here whether we can uh say the admin or user and then when you saving that time you can register but I will not going to do that thing I think if we can do this right so just click on the register here it's already a user and this is another user registered I actually change in the uh database and this admin is one right so right now these are the admin user these are the user right normal user so now when we just going to add going to login into the as a admin user I want this type of Link like admin home or when we just going to login as a normal user I need these type of things like a user at the rate email.com and login here so here should be normal this type of things okay so here how we can separate these two things so I need a middleware and actually middleware will do all the things so simply ah just create a middleware for this application so PHP or tuition make middleware okay and here my middleware name is is admin because I will check whether it's admin or not that the reason I am taking this type of name so app HTTP model app HTTP meeting layer is admin created successfully so just go app HTTP manage inside HTTP you have a middleware this is admin is added all right so now first thing first I need to register this middleware first otherwise it will not going to access so here inside this route middleware actually I'm just going to add so simply just right here is admin and here simply write the namespace of this so app HTTP middleware so the front slash and then the middleware name or actually the class name and then class okay so middleware is registered now into the router section I register you can register anywhere and as per your requirement you can do this I already published a mailware video what is the difference between these type of things okay I just going to use uh this midi layer as a route middleware right now okay so I register and now I don't need this and here I just going to implement some logic if we create any or if we logged in as a admin then we'll add to Define request otherwise we will say something like you are not admin right so here you can see so if auth we know when we requested or when a logged in we will get the auth and when if Earth and user ok so it will fetch the user record from Earth and I want to get only the is admin parts so if you check this is the is admin actually I am just going to use this is admin equal to equal to 1 that means it is a normal admin user okay then I will say that it's a return request means I will just proceed this request otherwise I will restrict the access else I will say um I will redirect to the home page okay so simply write here return and share write redirect and here just add the name home actually this home is appearing from here name and here just write something so if I just redirect you just should know why it is redirecting so I just going to add one error message and simply write um you are not admin or something you don't have access or you have not access you have no admin access OK that will be great okay so I just print the message so when any a normal user requesting these things okay now just go to the controller so if you just go to the controller I just creating one thing here actually it's calling the index home and I just are going to create another function for admin so public function and here write admin home okay so this is the function you can write anywhere you can create this function in different controllers so I just not going to create any another controller I just using the home controller and here I am writing a return view I will create a different view so here ah just go to the home page so views and here you will see the home page so just copy this and paste it here and paste ok so it is duplicating now I just going to change the name and here and just write not home actually I will create admin home right so simply write admin home ok so let us copy this I will actually call these things from home controller okay so now just to write here uh admin home okay so when we'll call this admin home then it will redirect this admin home okay so adminhome dot blade dot PHP and we have another home dot blade dot PHP so you will not you are logged in actually this is the admin home so here simply I just write one thing suppose inside the AC tag I just write ah welcome or you can write welcome to the admin dashboard or something welcome to the admin dashboard and just copy this and for user instead of admin we will write welcome to the user dashboard okay great now ah just check this when we login as a user so I actually login as a user so refresh this welcome to user dashboard and if you log in then welcome to the admin dashboard will show right now I need to call this function from my web.php so simply just copy these things and here I just want this kind of Link like admin slash home right so I will just create this type of things that is slash admin home and here I just write admin dot home this is the name and here my function name is admin home okay so actually when you call this link that time this admin home will appear and that time adminhomeblade.php will call okay so we need to create the logic actually I just went to logout now and when you log in this function when you click on this login which function actually call Will calling the login function which are inside the controller auth control actually there is auth and this is a login controller okay so these are these are the controller which responsible to login any user now here you can see if you inspect that things so you have a email and another things you have in password okay so where is the password these are the password okay so we need to control these things email and password we need to take a sales request I just going to create a function so public function and function sorry function login okay and I will use the request here so first of all we need to add the request here so here just copy from the home controller and just use this illuminate HTTP request and then we can access the request actually this request will come from the blade file okay so login plate dot phpq St request file okay and here if you check if you are just going to DD these things and let's see dollar request not underscore actually I need simple things I need this request now let's check what actually coming so I just going to login user at the red email.com and password is something right and if you just click on the login then you can see inside the request will get all the things that is the one is token you know that what is the token that is a CSR token actually an email and password we are getting these things so buy that thing actually we need to ah create the logic okay so simple here I just going to take a variable so I will take all the input things into the dollar input variable and I will use the request reqe Q EST request all okay so here I will get all the requests actually and if you print the request all the things are here if you only print the request all then only this part are getting okay now I am getting all the things now I just first need to validate these things whether we are providing the perfect email or password or not so simply I just use these and here we have a so here we can use the validator so this validate and here just write the request and now inside the request will get other things so simply here just write email and email is where we will get the email we'll get all the things emails would be the first of all the required and the type should be the email okay so this is the syntax to validate email and password and password will be the require only okay so I do not know what is the pattern of the password so simply I just write here the password okay now if you rephrase this then we will not get anything because we are not printing anything okay so if you write here Echo something or DD the input I want to check only the input okay so simply ah just print this we are getting the input okay so our ok now I just going to login from here so to login we can understand so to login we know that we have attempt function so simply we will just call auth attempt okay a t t e m p t auth attempt is the function sorry or the attempt and here I just going to say that is inside a attempt I just going to add array and here uh simply a r r a y array and I just want to create a array and there will have a email and something and then the password ok so email will get from this dollar input variable so simply write dollar input and here we'll get the email so here you can check this is the email we are getting as an array format and this is the password we are getting as an array format and all the things are present in the input um email sorry password okay so this will check whether we are getting the perfect things or not else if not then we will and simply redirect to the login page so actually this is the login page if not provided proper things so simply I just going to return redirect to where redirect to the login page okay so I just going to use the route okay so right now if you check what are the available routes you have so simply write PHP Artisan route list OK so you all have this route because of these auth routes okay so you can see there is a password password confirmation you have a login login you can has a use a gate or you can write as a post okay so all the things are from the login control actually we are overriding this login function now okay so you can see the post login login controller actually I am overwriting that things now ah just go here and simply just call the login okay so simply write login and I want to say some message why not logged in so simply here you can write um error and simply print input input proper email or password OK that's simply message right now inside here you can log in both user and the admin right simple user or admin now I need to check if these things if actually this part became login this atom function is responsible for login now I just redirecting if auth after login will get all the things in the auth right if auth user okay is is admin is admin then I will do something otherwise I will do something so if this is the admin user I will redirect to the admin part otherwise I will redirect to the uh this home part okay so now just add the logic so simply I just write return redirect and here I just going to move to the normal user so simply write route home so this is the name is home and otherwise I will redirect to the admin home ok so simply here I return redirect same actually return redirect and route and here simply I just provide admin home that's it okay now I created this easy admin so what is the purpose of that things I created I registered but how we can call this I added this middleware as a normal route middleware OK route level middleware I add it so I can add this Mailer in the route label so here is my route so here actually inside the admin home I can add one things I can call the middleware so simply write middleware and here I will just provide the mid layer name which I created the aliases ok so just kernel and is admin this is the things I need to call ok so here I just write is admin that's it right now if I just going to paste this what actually happened so simply just to go from the beginning so hard refresh okay so page are loaded now first I will login as a admin user so simply not register just go to the login and here write admin at the rate gmail.com Gmail or email email admin at the rate email sorry email.com and password let's see whether we are getting the admin dashboard or not so here welcome to admin dashboard okay actually admin can access the user panel there's a reason if you just go here you can go okay so we'll come to use the dashboard actually you can Dynamic even make it Dynamic when you just go to the um when you create your application actually this is the admin home okay and if you just go to the login page okay so when you are admin you can access the user dashboard this is fine and if you don't want to do you can create a separate middleware for each user also right and if you are just a normal user okay then you should not access the admin path so if you just log in and you are as a user dashboard and you are now trying to check the admin then it should not work otherwise admin Parts should not work from the normal user so just and type here and here you can see these are not going going to happen actually these are redirecting the home actually we are not getting the message there's a reason is getting problem and now if I just go here it's writing from here and if you have no admin access so if you are not admin that is setting that's it you have no admin access actually I need to print that things so just go to the normal user home page and just copy this and I just going to print that things so here this is the error so simply ah this is the if session error then print here error and I just going to change the class name okay now if I just going to use or going to access the admin home that time you have no admin access this message is showing okay as this is the the normal user type so you cannot access the admin type user actually we are applying this midi layer here okay and further if you want you if you want to create a another type of user and if you want to apply restriction you can add you can use the groove routing and inside the group thing you can set your all the URLs which you want to rest it you can do in this way so I already published the middleware group routing and all the things like related to middleware you can check this and now and now please comment if you have any questions about this video or not so if you want to create any multi-user login system I think you will not get any problem after seeing the full video right so please like And subscribe this channel for upcoming video and hope you will like this and you will share with your friend thanks for watching this video have a good day
Info
Channel: Learning Points
Views: 28,318
Rating: undefined out of 5
Keywords: multi auth using middleware, multiauthentication using middleware, laravel multi login system, how to separate admin and user dashboard, how to create admin login, how to create admin and user login separately, laravel ui, laravel ui auth, laravel ui auth tutorial, laravel ui login, customize laravel login, laravel auth login, laravel auth login tutorial, laravel middleware, laravel middleware user roles, laravel middleware admin, admin middleware, how to create middleware
Id: ckh_AXlMLgo
Channel Id: undefined
Length: 34min 48sec (2088 seconds)
Published: Sun Feb 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.