Laravel 8 Blog Admin Panel - 3: Setup Admin & User Login with Middleware & redirect | multi auth

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys we are continuing our laravel blogs series so guys now in this video we are going to learn about how to set up the admin panel authentication okay so as we have seen that we have done with the login and registration process so guys once you log in then only you can access your dashboard if you are not logged in then you should not be able to your access admin dashboard okay so right now you can see that we have not logged in but you can access your admin dashboard right so let's make this as authenticated all the admin routes okay so let's get started guys so first let's move to our editor and let me just close this files okay so first up guys let's move to our routes and go to web.php over here as we as we have created a admin route and inside this group only we are going to add the authentication part okay so now guys let's give the middleware over here so you just need to call that middleware function and inside the array auth okay so let's give our arrow and call middleware function and insert this just assign the array and call your auth so this is auth is your laravel built it up built-in thing okay so you just need to call that means it will check for the authentication so now guys now this allows has been authenticated if you're not logged in you cannot access this dashboard okay so let me just refresh and you can see that it redirects you to login page so let's try to access once again admin dashboard so it retires you to the login page so guys now let me just login one two three four five six seven eight and let's login so here you can see that you are back to dashboard okay so now you can access your admin dashboard let's refresh perfect so everything works fine so guys this is how we have set up with the authentication part for our admin dashboard so now guys coming to the main part so i have logged in as a normal user that is ved prakash n okay so he is just a normal user but normal user cannot access your admin dashboard right so we have to create one admin user and you should give a role for him that he should only access the dashboard normal user cannot access the dashboard okay so guys now let's set up that part so first up let's get back to our editor and i will just show you my user table and here you find no column called as a role as okay so let's add one column let's get back to our editor and give a command so php artisan make make colon migration where add role underscore as two users table okay and the table name just give minus minus or hyphen hyphen is equal to users table okay so users which means that this user table name okay and hit enter so if you hit enter so created migration so let's go to our database migration you'll find that perfect so here you can find that users right so you get the up and down and here you need to create a dollar table where i want to keep that as integer value which has a column name as role as and we want to keep this default by default it should be a zero value okay and now guys let's move below at this down function you just need to add the drop column of this roll as okay so okay let's remove and paste that so once you roll back it just drop this column from that user table okay so it's a setup part so now guys let's migrate this php artisan migrate hit enter so it's migrated now let's check this column rule as so refresh your browser and you can find that at last you can find that role as table i mean column has created so now guys let's get back to our application and set up the middleware so let me close all this let's get back to our application and now let's register one user as admin example.com okay register admin oops admin admin at example.com and here password will keep as one two three four five six seven eight seven eight okay let's register cool so admin is registered now now let's change his rule as one so let's get back here you find the admin and let's make his role as one guys so right now we are making it from here but if you require you can just build that in your admin dashboard and update his role as one if you want to make a specific user updation for admin okay so let's get back to our application and start with the setup of admin middleware so guys now let's create the middleware php artisan make colon middleware name called admin middle where perfect admin middleware hit enter so middleware created successfully so now guys let's move to app http middleware you can find the admin middleware just now we created it by the command okay so now guys coming to this handle function over here you go you are going to check if admin is there means you are going to handle that okay so let's handle this first and then we will register this admin middleware or just register first and then handle this request okay so first uh let's register it so you need to just go to your app http and kernel.php and move to your route middleware okay so here is our route middleware and just need to register one okay so let me just copy this first and paste at the last and let's mention is capital a dmin okay so is admin if it is admin means you are going to handle app http middleware and your middleware name that is admin middleware let me just copy the class name so this is my middleware copy and paste the call class okay so now this is your admin middleware is registered now you can close this okay so now let's handle the request guys first user if condition and check authenticated user if authenticated user is there then you are getting n if else so in this if authenticated user his role underscore as okay so authenticated user role as so this rule has nothing but your column field so here your table column name role underscore as okay so you are setting that and you are checking is double equal to 1 so 1 means admin let me just give a comment over here 1 is equal to admin and 0 is equal to common user okay that's it so this is your difference and if you want you can just set up with five or you can just give your random whatever okay names like admin or super admin however if you're required you can create it so guys now just let's get back so if you're registered like if you're a admin then let's handle this request perfect so if you're not an admin let's redirect redirect function with i mean we have to return this redirect where you want to redirect you can just redirect on the home page or any page you can just move that so let me just redirect on the slash home and let's send the message so with the status or you can add a message comma and you can tell access denied as you are not an admin that's it so and coming to this if else part like if you're not authenticated if you have not logged in you can just tell which please login first okay and you can send this to slash login so guys we have registered that admin middleware right so is admin so this is admin we are going to check in our web.php so at your middleware you just need to add is admin okay so this is one middleware was auth so which checks the authentication and one more middleware vh we are adding so that user should be admin okay so that part we are adding it so now guys you can just go and let's check the output for it so right now you can see that admin has logged in right so you will be able to access the admin dashboard let me refresh okay middleware auth not found on what admin middleware let's import that class guys so let's go to our admin middleware this auth is not found so you can just import that and here it imports okay so now let's refresh once again perfect so you can find that you are able to access the admin dashboard now let's log out guys and login as a common user so with gmail.com this is just a common user it's not an admin and let's login so you can see logged in successfully okay so it is logged in and now let's try to access the admin dashboard refresh you can see it's redirects you to the home page but we did not see the message let's check the message also okay on the home page so let's move to this home let me go to that route and here is our home and it goes to home controller at index so this page is called over here so here you can see that message is set to status so you can copy this and you can just change that instead of message to status okay so let's follow up with our level this part okay so yes here you get back and try to access admin dashboard let's refresh access denied as you're not an admin okay so you're not admin let's login as admin and check the status login admin at example.com change the password login okay so now let's move to admin dashboard and you can see you can access perfect so nowadays we have successfully set up with our admin middleware part okay so which user should access the admin dashboard and which user should not access the admin dashboard so now guys coming to the third point let's redirect the pages like if you log in as an admin you should move to admin dashboard if you log in as a normal user you should go to home page okay so that is a simple code guys just you need to move to your login controller okay so let me close all the files not required app http controller auth and move to your login controller all set so at this to redirect to let's comment this and call the authenticated function public function and authenticated function check in if condition where you can tell authenticated user his column role underscore as is double equal to 1 so you know right this one is equal to admin so if it is admin you can just redirect him return redirect with the status at the u.s status and the message welcome to admin dashboard okay and where you have to redirect let's copy the path so i just want to redirect at this admin slash dashboard all set if else if it's a common user you can just redirect him login successful that's it okay and you can just redirect into the home page all set guys okay if you require you can just give this the condition as 0 which means you can 0 is equal to user so in case if you are banning also like status so you can just redirect them directly okay and let's keep the safest part one else condition if mismatch with your rules something like that you can just redirect them to the home page and without any message and all okay so okay so now guys let's test it okay let's log out logged out and here also let me see refresh okay it's asking for login so now guys um let's move to this and let's login as an admin and he should be redirect to admin dashboard let me just login admin at example.com password12345678 log in so it should move to admin slash dashboard auth not found where you can just find that in login controller oops we missed out to import the class of this auth so just import class and the class name you find this so now let's get admin password one two three four five six seven eight let's click login perfect so if your admin you are directly moved to the dashboard and now let's test with the common user okay let's log out and let me login as a normal user with at gmail.com password let's login good so you can see login successful and you are not redirect to admin dashboard let's try to access admin dashboard you'll be redirected access denied so guys we have successfully set up with the admin middleware that is rules and permission to your admin dashboard so guys in this video that's it and let's continue next video about the laravel blog series so thank you once again guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 26,716
Rating: undefined out of 5
Keywords: fundaofwebit, laravel 8 login admin and user, laravel 8 admin login, how to make custom admin login in laravel 8, how to set middleware in laravel, how to make admin login with middleware in laravel, laravel admin middleware login, admin login in laravel 8, how to make user and admin login using middleware in laravel, how to redirect auth user to different page, laravel 8 admin and user roles
Id: h9XAs14d63A
Channel Id: undefined
Length: 18min 30sec (1110 seconds)
Published: Sun Feb 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.