Create Multi Auth using Guards in Laravel Fortify

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello everyone my name is harish kumar and welcome to our channel multi multi-oauth with laravel 45 is one of the most requested topic in this channel because of so many requests i have decided to create a video tutorial on this in the previous lesson also we have implemented the multi-guard authentication system and multi-guard authentication with laravel 45 is also same with few small changes in case if you also want to learn about laravel 45 package i have already created a series on this laravel 45 package i will put the link of it in the video description you should watch that also now let's start so in the terminal let's create a fresh new laravel app so laravel new let's say app name is multi oauth 45 it's done now cd multi or 45 and get in it get add and get commit initial now to create a quick authentication i'm going to install laravel themer package as i have already mentioned in the previous lesson this laravel themer package is optional it is just to create quick authentication scaff folding and multi-theme support instead of this you can also install the laravel ui or bridge package so now let's install this so i'm going to run this composer require command in the terminal it's done next let's publish this configuration file now let's open this in the editor and next i'm going to i'm going to paste this code in the webpack.mix file right here now close this next i'm going to add this theme middleware in the app http kernel.php right here in the route middlewares like this great now themer package configuration is done now next let's install the laravel 45 package so let's run this composer require command is installed and now publish the fortify resources and now all the files are published now let's create a theme so php artisan make theme let's say theme name is frontend and i'm going to use bootstrap and i do not want to use any javascript framework so skip this because here we are using laravel 45 so i will publish these views only great it's done and next i'm going to add this front-end theme name in the config theme.php right here in the active theme is done and next here i'll run npm install it's installed now here i'll run npm run dab and hyphen iphone theme is equal to front end it has installed the additional dependency so let's run the npm run dev again now assets are compiled successfully so let's see this in the browser so first i'll create a virtual host for this app so here i will run valet link now let's see this in the browser and here we go we have register and login pages here i got a error actually i forgot to add this fortify service provider in the config app.php right here in the providers array i'm going to add it right here so duplicate this and here i'll say fortify service provider like this is done and in the 45 service provider right here in the boot method here we need to add views for register page so here i'll say 45 and register view and here i'll pass a view name let's say that is oath dot register or the register it is going to use the view from the themes front-end theme in the views oath here is the register view now go to browser refresh and here we go we have register view similarly we need to add a view for login page so in the fortify service provider here i'll add 45 and login view and here i'll pass both dot login now refresh and here we go we have login page now let's register a user first we need to set up a database so let's create a new database to create database here i'll run valet db create let's say database name is multi auth 45 and database is created next in the editor let's navigate to dot enb file and here database name is multi youth 45 username is hero lab and db password is password it's done now let's close this php artisan my create next let's register a user so harish hurrish at example.com and password confirm password and register and it has redirected to home page but we do not have home root so let's create this so in the roots in the web dot php here i'll say root view it is going to be home path and it is going to render home home.blade.php and because it is only accessible to logged in user so here i'll add middleware auth now refresh this page and here we have home page let's logout let's test this login so reach at example.com and password we are successfully logged in now let's create an admin theme so php artisan make theme and here theme name is admin now this time i'm going to use tailwind and skip javascript framework and i'm going to select views now npm install is done now npm run dev hyphen f1 theme is equal to admin it's done it's compiled successfully now to create admin routes in the roots directory i'll create a new file that is admin.php and now i am going to include this admin.php right here in the web.php so here i'll say require admin dot php i generally prefer to register routes from the root service provider but tutorial purpose it is fine if you also want to learn about how to register routes from the root service provider i have created a video on this i will put the link of it in the video description you can watch that also now next let's create a admin root so here in the admin.php and here i'll create a root group so root group and make sure to import this class this one and its prefix is admin and here i also add middleware and in this middleware i will specify theme admin theme so this will render the only views file from the themes admin theme views from right here now here i'll create a login root so here i'll create a root view and i will pass root path is login and it is going to render view auth dot login and its name is login like this now let's see this in the browser so here i'll navigate to admin slash login and here we go we have admin login page now here in this navbar you can see the logged in user i will fix this in a moment first let's create a admin guard so in the config auth.php and right here it has specified that default guard is web and here in this cards array we have web and api guards now let's add a admin card so duplicate this and here i'll say it is admin card and here i'll specified provider admins and now we need to create these admin providers right here in this provider's array so let's duplicate this and here i'll say provider name admins and it is going to use driver applicant and model is admin model now we need to create this admin model so in the terminal i'll run php artisan make model admin and employ for migration and f4 factories is done now in the editor now let's close this now let's open the database migrations here is the admins table here we have users table so i am going to copy these column from the users table and paste that right here in the admins table and next in the factories i'm going to copy these values from the user factory and paste that in the admin factory right here and import this string class this one now let's close this now let's open the models admin model and to allow the authentication for this admin model we need to extend this admin model with the authenticatable class this one because all the columns of admin and user tables are same so i'm going to copy all of these code from user model and paste that here in the admin model and change its class admin it's done now close all of these then the php artisan my create to create admin table next i'll create admin user so php artisan tinker and he'll say admin factory create and here i'll pass email is admin at example.com and here we go admin user is created now next let's implement the login by admin user from this admin login page so in the admin root similar like in the previous lesson here we can create a new route which is going to send post requests to login page and from this route we can implement the admin user authentication same as we did in the previous lesson but instead of this i want to use laravel 45 controllers so let's go to the vendor and in the laravel 45 package and here in the roots directory here we have all the roots defined in the laravel 45 package for authentication so from here i'm going to copy this route which is sending the post request to this login route for authentication so i'm going to copy this and paste that here this limiter value is used here is not used here so let's remove it i also forget to add name in this root group so here i'll say name admin dot next in the themes admin theme in the views and in the oauth login.blade.php right here in this form here i'll say admin dot login it's done now close this and here it has guest middleware we need to pass admin guard like this now let's refresh and try to login admin at example.com and password and login authenticated session controller does not exist i forgot to import this class this one from the laravel 45 package and refresh it now if here if we try to login it will still not work let's see admin at example.com and password login and here it is not working the reason for that if we see this authenticated session controller here in the construct method it is using stateful guard and if i dump this here to see which guard it is using and now let's try to login and you can see it is using webguard and we do not want that instead it should be the admin card if we see this stateful guard class it is an interface so how we can pass admin guard right here in this authenticated session controller if we see the fortify service provider of the vendor laravel 45 package in this fortify service provider i will search for stateful guard that is imported right here and here it is and here is the stateful guard class and laravel is using dependency injection or you can say service container this service container is the very powerful tool of the laravel framework if you want to learn about the laravel service container write down in the comment section if i try to explain this in easiest way whenever laravel is looking for this class this bind method will return both guard class this one and in this oauth card you can see it has passed 45 guards from the configuration so if we go to configuration 45 configuration this one by default it is web so here its value is web so if i change this web to admin then so here it will return the admin card but i do not want to change it here because we are using multi guard so what here i'll do let's close this first let's remove this and close this and in the app providers fortify service provider right here in this register method here i'll say here i'll check if request is admin slasher so if any request which has admin prefix in that case i'm going to set config and i'll call set methods and here i'll pass this fortify card set this to admin like this is done now let's try to login in the admin login page so admin at example.com and password we are successfully logged in but it has redirected to the this home page instead it should reduce it to the admin slash home page first let's create this admin home route so so in the admin root here i'll say root view and the path is home and it is going to use home view and here i'll pass middleware port admin and its name is home now refresh and here you can see this is the admin user and if i go to the home page this is the different harish user and here in the admin home page this is the different user which is a admin user before fixing the redirect issue let's create a admin logout as well so let's close this and from the laravel 45 roots roots.php from here i am going to copy this and paste that right here and i am also going to add this auth admin middleware to this logout root like this and next in the themes admin layouts app.blade.php right here the root is admin dot logout and in this section here also admin dot logo great now let's log out it's working and we are logged out and login again and try to login as admin ad example.com password and it has redacted to the front login page actually behind the scene first it has redirected to the home page because this home page is using the web guard and here the user is not logged in so it has redacted to the this login page now if i try to visit admin home page and here we go we are logged in as admin user now let's fix this redirect issue so if we open the country 45 dot php and scroll down here we have home route service provider home here is the home path so after login it is redacting to this path so we also modify this in the 45 service provider right here just duplicate this and here i'll say 45 home it should be the admin home this one now this time let's log out and navigate to the admin login page and now if i try to login as admin at example.com and password and this time it should redirect to the admin home page and here we go we are redirected to the admin home page and if i try to visit the home page it is not allowed now if i visit to the admin login page we are able to access this admin login page instead it should redirect it to admin home page if a user is logged in and we can easily fix this issue we just need to pass guest middleware right here but before adding a guest middleware i also want to show you one more issue let's log out and log in with this normal user i reach at example.com and password and now we are logged in now if we try to visit admin login page right here you can see it has printed the name of the normal user which is incorrect that is because if we navigate to the themes admin layouts app.play.php right here and here as i mentioned in the previous video if we do not provide any card name it is going to use web middleware so to fix this here we will specify guard admin and now refresh now it is login and register pages link and if we see the home page we are logged in as harish so great it's working similarly to print user here we should specify the guard admin like this here you may think it is very long code to type so what you can do you can create a helper method like admin this admin helper method returns this auth guard admin user like this if you want to learn how to create this helper method i have explained this in the previous lesson you should watch that video now let's look out and login with admin login page for admin at example and password and here we go we are logged in now from here if i try to visit admin login page it is working instead it should redirect to the admin home page so to fix this in the admin root here i'll add middleware guest and then pass admin card like this now let's open the admin login page and it has redacted to the this normal login page actually behind the scene what is happening if we try to visit admin login page it has redirected to the home page because this home page is using the webcard and the normal user is not logged in so it has redacted to the login page so to fix this we need to modify the guest middleware so let's open the http kernel.php right here in this guest middleware it is using the redirect to authenticated middleware let's open this and right here and right here we have got so here i'll say if dollar card is equal to admin in that case it should redirect to to admin dot home like this now this time let's try to visit admin login page and here we go we are detected to admin home now let's log out and from here if i try to visit admin home page it is redacted to the normal login page instead it should redirect it to the admin login page now to fix this we need to modify the auth middleware this one let's open this now right here i'll say if a dollar request is admin star if a url has admin prefix in that case it should return to the route admin login like this now let's try to visit admin home page and here we go we are directed to the admin login page great now we have successfully implemented the multi-guard authentication with laravel 45 now let's review everything one more time if we are not logged in if i try to visit admin home page it is not allowed it is redirected to the admin login page and if we try to visit home page it is not allowed it has detected to the normal login page now let's login as admin [Music] user and here we go we added director to the admin home page and from here if i try to visit normal home page it is not allowed we are redirected to the login page similarly let's logout and login as normal user sorry example.com password great we are logged in from here if i try to visit admin home page and it is not allowed and detected to the admin login page great everything is working so this is all in this lesson thanks for watching if you like the video hit the like button share this video and don't forget to subscribe us see you in the next lesson [Music] you
Info
Channel: QiroLab
Views: 7,388
Rating: undefined out of 5
Keywords: multi Auth, laravel multi Auth, laravel multiple authentication, laravel multi role authentication, multi role auth, multi guard auth, multi guard authentication, learn with qirolab, qirolab
Id: x8PfZqMXn5U
Channel Id: undefined
Length: 26min 45sec (1605 seconds)
Published: Thu Mar 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.