Laravel 8 Multi Guards Authentication

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello viewers in this video we are going to see how you can make multi-guards authentication in laravel 8 we will see the way you can make this step by step we authenticate users with separated tables users admins and doctors table as you can see in resources views folder we have only welcome blade php file we need to install basic laravel auth ui make sure your xampp is running navigate to phpmyadmin and create new database and rename to my underscore laravel 8 underscore multiguards once database created copy its name and go to dot end file and update db underscore database variable to my underscore layer of l8 underscore multi guards then save the file open terminal and run php artisan serve command as you can see we are on laravel 8 but we don't have login and register links here by looking in our controllers directory we have only controller php file which is controller provided by laravel so go to google and search for laravel ui then click on gethub page here is the installation guidance for this back to visual studio code and run composer require laravel ui command in terminal then after this run php artisan ui bootstrap dash dash auth command as you can see the authentication scaffolding has been generated successfully but if you need better looking login and register layout you may run the above command to compile fresh scaffolding so when authentication scaffolding generated we see that there is a new auth controller files inside app controllers directory these files used in login register and reset password functions if we refresh this page you see the login and register links appear on top right of page this is a default login page and then this is a register page in login and register process the login form will use this login controller while register form will use this register controller in our database we don't have any table on it we need users table by default as you can see in database migration folder we have the users table schema looks like this now go to terminal and run php artisan migrate command as you can see we have now users table inside our database this table is empty now we can register new user by using this form let's add new user as you can see new user created and redirected to home page by looking in users table we see this new registered user let's log out now then login with this user credentials as you can see this user redirected to home if email and password matches to the records inside users table friends now you see that login and register work perfectly for the purpose of this tutorial we are going to redirect authenticated user to the home page with the user prefix like this to do that let's create our own controller by entering php artisan make colon controller user slash user controller command in terminal this is our generated user controller file go to web php file then import our new user controller we have created on this web php file this is the default home route for authenticated user as you can see it use home controller but for us we don't need this anymore just comment home root let's create root prefix group for user inside this group we will create other two root middleware groups this group middleware will protect routes using guest middleware and this group middleware will protect roots with auth middleware let's see those middlewares as you see in kernel.php file we see those middlewares auth and guest middlewares for auth middleware we have authenticate.php file this will redirect an unauthenticated user to the specific login route by checking prefix if is normal user this will return user login root if is admin this will return admin login root and if is doctor this will return doctor login route and for guest middleware we have redirect if authenticated php this one will redirect an authenticated user to a specific home route by checking guard name for example if authenticated user is a normal user this will redirect him or to user home route if authenticated user is admin this will redirect him to admin home route and if authenticated user is doctor this will redirect him or her to dr home route let's continue create normal user route view for login register and home pages let's create these views blade files now design login page by using bootstrap classes our login page form will contains email and password input fields let's also design register page this register page form will contains name email password and confirm password input fields so our user login and register page design have been completed let's show something if you look the url of this login and register links they are using the default user routes but we need to use our new user routes now go to welcome blade file and update login and register links let's see if we click on login link we go to our new login view and also if we click on register link we go to our new register view let's add some links on these forms now our login and register form layout are ready to use back to web php file as you can see we have now normal user urls which have user prefix we have to update the following files on authenticate.php file we have to change this login route to the new root name also on redirect if authenticated php file we have to change this return redirection the new home route of normal user this is the new home route for normal user back to web php file let's add post root for create new user here we will use our user controller class file then use this name root to the register form action attribute don't forget to add csrf to this form let's make this create method in usercontroller class but before continue don't forget to import this on this controller just import user model and import this auth facade here now back to create method let's start validating inputs name must be required email must be required be an email format and unique into users table in email column password must be required with minimum of 5 and maximum of 30 characters confirm password must be required with minimum of 5 and maximum of 30 characters and same to the password value so let's display these validation errors on register form we are going to add error spans on this form that will display validation errors under each field let's add this old function that will help us to hold last entered value on each field now add error spans under each input field let's test as you can see validation errors appear under each input field let's continue if validation success let's insert new user into users table if user inserted successfully let's display success message on register page but if not inserted let's display error message on register page to show success and error messages we have to add the following if conditions on this page let's test as you can see we have success message that show us the new user has been successfully registered if we look into users table we see new registered user now our user registration process done let's make login process too back to web php file and add new post root for checking login user now add this name root name to login form action attribute don't forget to add csrf to this form let's make check method in user controller class firstly validate inputs email must be required be in email format and exists in users table password must be required with minimum of 5 and maximum of 30 characters let's display these validation errors on login form add error spans that will display validation errors on under each input field let's test as you can see we have validation errors displayed under each input field if we try to enter no registered email address we see the message that tell us that the selected email is invalid which means that the email is not exists in users table let's change this validation message as you can see the validation error message has been updated let's enter exists email address no error displayed now let's turn off autocomplete on this form this is before and this is after no autocomplete appears let's continue if validation success let's check entered user email and password you can also specify guard name on this attempt if you want because users table use webguard we don't need to specify guard name here this will work too so if entered email and password matches to the registered user in users table we redirected to user home root if not redirect it back to login page with error message let's show this fail message on login page just add this if condition here let's test if we enter email not exists in users table we see the message that tells us the entered email is not exists in users table let's use exit's email no error appears if we enter wrong password we see the error message that tells us there is incorrect credentials as you can see this user redirected to home route with the user prefix let's design this user home page we will use bootstrap classes to design this page let's add table to display logged user name and email if we refresh page we see the logged user name and email displayed into table let's add log out link here add new post route for log out here into this middleware group now add this anchor tag for logout link here this form must be hidden on page then make this logout method inside usercontroller class let's test now we have logout link here if we click on logout link nothing happened let's see the problem we didn't add id attribute to this form let's test again if we click on logout link we logged out and redirected to index page let's login with other user now we are on new user home page if we try to access user register page when we already logged in this will redirect it back to home page which is good and if we try to access user login page when we already logged in this will redirect it also to home page let's log out if we try to access home when we don't logged in this will redirect us to login page which is good let's login now we are on home page if we try to go back by using browser back button we back to login page which is not good how to prevent going back to login page by using browser button when we already logged in just create new middleware by entering php artisan make colon middleware prevent back history command in terminal this will generate preventback history.php file inside middleware directory now let's add the following codes inside this handle function register this middleware in kernel.php file let's use this middleware to our roots add this as a second middleware to the guest and off middlewares let's test log out now then login again now we are on home page if we try to go back to login page this will not redirect us to login page because we already logged in that is why we still on home page which is fine let's log out now if we try to go back to home page this will redirect us to login page because we already logged out which is good friends as you can see the login and register process of this normal user has completely done one thing i can show you here is that you can specify a guard name to every function you made let's see on this guest middle where you can specify a guard name like this and also you can specify guard name on this auth middleware like this also in user controller you can specify a guard name like this and also on displaying logged user info on blade you can specify guard name like this let's see if works or not now check login function now check logout function friends you see that everything works perfectly now for the purpose of this tutorial we are going to make multi-guards authentication by looking into database we have only users table for normal users this means we use web guards to authenticate these users now let's add separated admins table for admin make admin model by entering php artisan make colon model admin dash m in terminal now we have admin model here this model will have the same structure to the user model and also we have generated admins migration file for admin model just copy user model content and paste into admin model then change class name to admin please don't forget to import these things here that why i copied and paste like this for our admins table structure will be the same to users table but with little changes let's copy this and paste to admins schema then add phone column to this table add this column name to admin model fiable array now let's run php artisan migrate command as you can see we have now admins table in our database for admin we are going to insert default admin user manually it is no need to make admin registration process in this tutorial now we have default admin let's copy hashed password from one of users table user friends we have now an admin now to add new guard we have to go to config auth php file here you will find the way you can add your our guard let's add admin guard just follow the steps now we are ready to use our admin guard back to web php file we are going to add new admin root prefix group for admin users also this group will have two root middleware groups inside it as you can see we specify guard name on this guest middleware the same to this auth middleware let's create root views for login and home of admin user we need only login and home views on admin now create those views blade files let's design login page by using bootstrap classes this form will have email and password input fields let's test if we try to get admin login page we are now have this login page layout now our admin login form is ready to use back to web php file now let's create admin controller by entering php artisan make colon controller admin admin controller command in terminal let's import admin controller here don't to forget to update authenticate.php and redirect defauthenticated.php files by adding this admin routes for this we have to check if root have admin prefix then return admin login route and here we check if a guard is an admin guard then redirect this user to admin home route back to web php file and add post root for checking login admin now use this name root to the admin login form action attribute don't forget to add csrf to this form then create check method inside admin controller class again remember to import admin model and auth facade on this controller now validate inputs email must be required be in email format and exists in admins table password must be required with minimum of 5 and maximum of 30 characters let's add errors spans on admin login that will display validation errors let's test as you can see the validation errors displayed under each input field if i enter the email not exists in admins table we have this message that show us the selected email is invalid which means the email is not exists let's change this validation error message let's see the results as you can see the message has been changed but if we enter exists email we don't have any error message for this let's continue here we check the entered email and password of this user as you see we specify guard name here if matches to the records in admins table we redirect this user to the admin home route otherwise return this user to the admin login page with error message let's add if condition on admin login page that will display error message when login process failed now let's test let's enter exists admin email and wrong password we have this error message let's enter real exists email and password of admin as you can see we redirected to the admin home page let's design this admin home layout here we will use bootstrap classes to design this page then add table that will display logged admin info as you see we specify guard name here let's test if we refresh this page now we have admin info displayed into table let's add logout link here back to web.php file and add new post root for logout inside this middleware root group now add logout anchor tag in our table let's make this logout method in admin controller class also we specify guard name here let's test if we click on logout link we logged out from admin dashboard let's login again to show something if we want to back by using this browser back button we back to the login page which is not good how to prevent going back to login page when we already logged in just add preventback history middleware on these two root groups let's test now login again if we try to go back by using this browser back button we still on admin home page instead of going back to admin login page because we already logged in and if we click on logout and try to go back by using again this browser back button we redirect it to admin login page instead of going to admin home page friends as you can see our admin login process has been completely done we can also add more tables for authenticate users we create users table for normal users and admins table for admin which means we have two guards webguard and admin guard let's add another table for doctors just enter php artisan make colon model dr dash m command in terminal now we have doctor model and also we have doctors table migration file let's use this user's table structure to the doctor table then change a little bit by adding hospital column on this table for doctor model also make sure this import all classes of user model change this to doctor and add hospital column to this feeable array let's create doctor controller by entering php artisan make colon controller dr dr controller in terminal now doctor controller has been created back to web.php file and import this controller on it now we are going to add dr prefix group as you see we specify guard name on this auth and guest middlewares let's add some root views for doctor then create this views blade files let's design doctor login page layout by using bootstrap classes this doctor login form will have email and password input fields then design doctor register page this doctor register form will have name email hospital password and confirm password input fields whoops we forgot to define dr guard in config auth php let's do it now let's define dr guard now let's refresh this page as you can see we have now doctor login page and also register page because we have defined dr guard and used in our routes we have to update authenticate php and redirective authenticated php files on this we check if url has doctor prefix then return doctor login route also on this we check if guard is doctor and then redirect him or her to the doctor home route let's continue now add post root for creating new doctor then use this name root to the doctor register form action attribute don't forget to add csrf to this form now let's add this create method in doctor controller class remember to import doctor model and auth facade here on the top of this class let's validate inputs name must be required email must be required be in email format and unique in doctors table hospital must be required password must be required with minimum of 5 and maximum of 30 characters confirm password must be required with minimum of 5 characters maximum of 30 characters and same to the password value here we use old function to maintain last entered value let's add error spans to doctor register form for displaying validation errors under each input field let's test as you can see we have validation errors under each input field let's continue if validation success let's create new doctor into doctors table if new doctor inserted successfully into doctors table we redirected this to dr register page with success message otherwise display fail message in order to show success and fail message on register form we have to add the following if conditions let's test whoops we have an error this means we don't migrate our doctor's migration file no doctors table founded in our database as you can see we have updated this migration file but not migrated let's run php artisan migrate command in terminal as you can see we have not doctors table inside our database let's try registering new doctor again as you can see we have this success message that show us the new doctor has been inserted successfully if we look into doctors table we see that inserted doctor let's register another one if we use the doctor email that exists in doctors table we see this message that show us that email is already been taken now we have second doctor in table let's make doctor login functionality now back to web.php file and add post root for checking login doctor let's use this root name to the doctor login form action attribute don't forget to add csrf to this form now add check method inside doctor controller class validate inputs first email must be required be in email format and exists in doctors table password must be required with minimum of 5 and maximum of 30 characters now add errors spans that will display validation errors under each input field let's test as you can see we have validation errors displayed under each input field if we enter the email not exists in doctors table we see this message that tell us the selected email is invalid let's change this error message let's test as you can see the error message has been changed let's enter registered email no error displayed now let's continue if validation success let's check entered email and password as you can see we specify guard name here if this credentials matches to the data in doctors table redirect this user to doctor home page otherwise redirect him or her to the doctor login page with error message let's add if condition on doctor login page to display error message if login process failed let's test if we enter exists doctor email and wrong password we have this error message that show us there is incorrect credentials let's enter correct credentials as you can see we redirected to dr home page but we have an error i made a mistake of writing wrong guard name let's show you what i mean on this middleware root group i wrote two colons remove one if we refresh this page we see that we are now on doctor homepage let's design this page by using bootstrap classes add table to display logged doctor details as you see we specify guard name here as you can see we have logged doctor details displayed in table let's add logout link here back to web php file and add post route for logout inside this group let's add this logout method to doctor controller here we also specify guard name now add logout anchor tag here let's test if we click on logout link we redirected to index page if we try to access doctor home page we redirected low doctor login page because we are not logged in let's login now now we are on doctor home page if we try to go back by using browser back button we back to login page which is not good how to prevent going back to login or register page when we are already logged in by using browser back button just add prevent back history to both this two middleware groups let's test log out now if we try to go back by using this back button we still on this home page because we already logged in let's log out now if we try to go back by using this browser button we redirect it to doctor login page instead of going to doctor home page because we logged out friends are multi-guards authentication is completely done let's review all users if we try to access normal user home page we redirected to normal user login page if we try to access admin home page we redirected to admin login page and if we try to access dr home page we redirected to doctor login page let's login as doctor now we are on doctor home page if we try to access to the normal user home page we redirected to normal user login page also if we try to access admin home page we redirected to admin login page let's log out now then login as normal user now we logged in as normal user if we try to access dr home page we redirected to doctor login page also if we try to access admin home page we redirected to admin login page let's log out now login as admin now we are on admin home page if we try to access normal user home page we redirect it to normal user login page and also if we try to access to doctor home page we redirected to doctor login page friends this how you can make multi-guards authentication in laravel 8. you can add all routes that can be accessible by only logged users inside root group with auth middleware like this if you are new on this channel please subscribe in order to get notified when new video uploaded on this channel thanks for watching this video you
Info
Channel: Irebe Library
Views: 52,754
Rating: undefined out of 5
Keywords: irebe library, multi guards, laravel 8 multi guards, multi guards authentication, authentication, auth
Id: PqAaBo_I_a4
Channel Id: undefined
Length: 75min 0sec (4500 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.