SC001 - Laravel 8 Multi Auth based on Roles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello viewers in this video we are going to learn how you can make a multi-auth in laravel make sure your xampp is running go to phpmyadmin and make laravel underscore 8 underscore multi-auth database then copy this database name and back to rn file and update database name here and save open terminal and run php artisan serve command let's navigate this url as you can see we are on laravel 8 but we don't have login and register links here back to visual studio code and add new terminal here we are going to install our auth ui in terminal run composer require laravel ui command then after run php artisan ui bootstrap-auth command as you can see the authentication scaffolding generated successfully but if you want more and better looking scaffolding you may run this command for me it failed in visual studio code terminal but i can run this command in cmd as you can see it start installing and this will take a while depending to your internet but for me i don't need this because i will use my own login and register layouts that is why i close this back to visual studio code and run php artisan serve command as you can see we have not login and register links but when we refresh the page those links will appear on top right of page that means we already installed auth ui in our project this is our login layout but is not styled don't worry i will show you how to use your own layout and this is register page layout so back to visual studio code and go to migrations and find users table migration file then here add new column under email column and rename to roll then after run php artisan migrate command as you can see our tables have been generated into laravel underscore eight underscore multi-auth database and users table has also roll column back to visual studio code this is the default authenticated user home route let's create admin controller by entering php artisan make colon controller admin controller command in terminal then after create another controller for normal users and rename it to user controller at the top of web php file let's import those two controllers and then after import this auth facade don't forget this line first let's write root group for admin with by default can have auth middleware this admin has dashboard profile and settings pages also this group must have auth default middleware this user has dashboard profile and settings pages after created admins and users roots let's update user model file and add role column on this array under controllers we have auth folder which has login and register controllers then on login controller php file let's add the following function under this line but don't forget to add this line here here is our login page as you can see this form has login route but first let's create this functionality let's overwrite login function on our login controller file create public function login method here we check email and password of user if credentials are not correct redirect user low login page with error says the email and password are wrong but if credentials are correct check user role and redirect this user to the correct dashboard so this is our login function let's now update register functionality go to register controller php file as you can see we have create method here let's add roll column here by default let's b2 which means normal user roll number as you can see we don't have any admin and user middlewares then on terminal let's create is admin middleware file and is user middleware file in admin controller let's add this auth facade line here then update this handle function and check if user has role 1 which is admin then after let's register this middleware in kernel.php file also go to normal user middleware and add this line here and inside this middleware handle function add this and also register this user middleware in kernel.php file then back to our web php file and add each middleware to a specific root group is user to a user root group and is admin to admin root group another file we have to update is redirect if authenticated php file comment this and add the following if this authenticated user has role 1 will be redirected to admin dashboard and if this authenticated user has role 2 will be redirected to normal user dashboard so let's test now as you can see he have not any user inside users table let's register new user as you can see this user registered successfully and redirected to home as you can see we have user inside users table with role2 let's register second user also admin paul registered successfully let's change this user role and change to one which is admin role let's log out let's login with the user which has normal user role as you can see on url this logged user redirected to user dashboard but we don't have this method add these methods inside usercontroller class and create user and admin views let's add all methods and views on user controller class let's refresh as you can see we don't have the error let's add some text here with logout link copy this home layout and paste to the user's views and on admin views let's refresh page now as you can see we have this layout that shows we are on normal user dashboard let's test admin as you can see this user redirected to admin dashboard now we can add any input field on our register form let's add new column on users table let's rename this to favorite color update user model feeble array and add favorite color to it then back to register controller php and add this favorite color on validation and also let's add this favorite color on create method then update register view and add favorite color input field to register form let's test let's register new user as you can see new user has been registered with red color let's test if armed always work if we try to visit admin dashboard page without logged in this will redirect user to login page also if we try to visit user profile page without logged in we also redirect redirected to login page let's login as admin suppose we are logged in as admin if we try to visit a normal user root we redirected back to admin dashboard let's log out let's login as normal user if we try to visit any admin route we redirected back to user dashboard let's log out now if we try to back by using browser back button we back to user dashboard which is not good how to prevent back to user dashboard after logged out let's create another middleware and rename to prevent back history then update this handle function to the following then register this middleware to colonel.php let's add this middleware to our admin and user root groups let's test suppose we are logged in as admin if we click on logout and try to back to previous page this will redirected us to login page instead of going to admin dashboard page let's login again as admin if we are on dashboard page and try to back to previous page this will return us to login page which is not good how to fix this problem just back to web php file and create middleware group and add the prevent back history middleware to this group then after move this authroutes inside this group let's test let's login again if we are on dashboard page and try to back to previous page this will redirect it back to dashboard instead of back to login page because we already logged in now we can use our own login and register layouts here we have bootstrap 4 layouts for login and register we are going to use these layouts in this project in order to use this let's add these files inside our project public folder then back to our auth login and register view directory let's rename old login file then create new loginblade.php file under this directory let's add this html to it as you can see our login page became nice let's modify also register page also rename old register file and create registerblade.php file under this directory add this html to it as you can see we have nice register page layout then let's include our functionality to these layouts make sure action is correct make sure all inputs fields are included in this register layout form update this layout login form this means we don't have csrf token on our form let's test login as you can see this login layout works perfectly let's register new user as you can see new use has been registered successfully how we can create new user and prevent auto login to do that back to register controller file comment create method then create our custom register method here but before continue make sure you import this line here on top let's continue if user registered successfully let's return back to register with success message that shows that user has been successfully registered but is not registered back to register page and display error message says the use not registered let's display validation errors on register view as you can see errors can be displayed on register view then for response let's add these alerts here let's test as you can see we have success message that show us the user has been registered by looking into users table the user has been inserted then let's login with this user credentials here we go we are on user dashboard so friend this is how you can make muti auth system in laravel 8 if you are new on this channel please subscribe in order to get notification if we uploaded new video on this channel thanks for watching this video
Info
Channel: Irebe Library
Views: 20,601
Rating: undefined out of 5
Keywords: irebe library, Laravel 8 Multi Auth based on Roles, laravel authentication, laravel multiauth, laravel multi auth, custom auth, laravel 8 auth, laravel 8 authentication, login and register, multi auth, multiauth
Id: sKyMRdzYgXw
Channel Id: undefined
Length: 41min 2sec (2462 seconds)
Published: Fri Apr 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.