Laravel 8 Crash Course Part 13: How to make middleware in laravel 8, Admin Middleware-Access Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn about middlewares so what is middleware in laravel so middleware are nothing but the access or a control thing okay so for example if you click on post you can see that it's not going to post it's going to login so this is nothing but your middleware which has come over your access control so when it is authenticated or user is logged in only then only he can reach to his posts for example if we click on employee you can see directly he has inserted because it has not given a middleware or access control so the same way we are going to create a middleware for only for a few users like admin for example will take us admin thing okay so basically as normal register and login so let us login with way that gmail.com password login now you can reach to post okay so now this with prakash has logged in now let us try to log in with om prakash let's click on post om gmail.com you can see that um has logged in successfully okay so but i need only with prakash should login to this post and any other user should not log in so guys for that we are going to create a middleware so let's log out and start with the uplay project so let's move open your terminal give a command php artisan make colon middleware where i am going to tell admin middle where hit enter middleweight created let's go to app http middleware you can find admin middleware let's click on this and save let's kill this terminal so guys you need to write some code over here let's comment this and just type with the if condition first you need to check the authentication authentication check if it is authenticated so let's import this class guys auth so this is the class which we are using auth and then let's use another if statement where i want to tell authenticated user if the user is logged in and his role as is double equal to one okay so he's one means admin will be so let's specify that zero is user one is equal to admin right and after when he is logged in that time what you have to do let's copy this and paste let's give else return and redirect with status we'll send the message over here access denied as you are not a admin not an admin okay let's save and then if he is not on 30k then what you have to do give a else condition if the authentication is not checked that time you need to return the message as please login first save and now guys we need to see this role as from where is coming so this auth is nothing but your users table so let us move to our model inside this model user.php file is there so let's call that users role as so let's copy this and paste over here role as and then let's go back to our dashboard so let's click on the structure and now let's add one column so after remember token i'm going to add one column which is roll as and it will be tiny integer where i am going to give the length as 2 and we will accept that default 0 so whenever any one register it should accept with zero and accept this null okay and let's save this so that this is altered so whenever you try to access this login authentication it will call that column so let's close this user.php and now when we have created this admin middleware we need to register it somewhere so let's move to what we say kernel.php so inside this kernel you need to move below at the route middleware because we are going to use in the route so let's copy the same thing and we will edit it we will tell is admin http middle where you need to call your admin middleware so this middleware path you have to call app http middleware admin middleware of its class that's it so let's save guys and go to our web route routes web dot php over here so you you are using this auth middleware right for this post so whenever anyone try to access this post which means this post let's click on post whenever you access that means it's newton authentication so because of that authentication is coming now i want to add admin only should access that post so let's add this with the oauth you are going to add this admin copy this and paste over here that's it so let's see and let's get back guys let's click on post you'll be redirected to login and after login where you have to redirect to this post so for that let's go back to our login controller app http controller auth inside your login controller let's move over here to redirect function you need to write the code over here so let's comment this and what code you're going to write over here inside your authenticated user you can find one function let me just attend [Music] decated let's see that see here guys so let's copy this function okay so let's close this copy that and paste below this as simple and now guys you need to create we are not going to request any request from here we are going to directly check that let's type if condition if authenticated user his role as is equal to single quotes one so if it is the admin so one is equal to admin then what you have to do return return and redirect with status where you have to go welcome admin will give okay so where you will be redirected to post slash posts so which means this url posts okay so over here and if you're not if you're not an admin then let's use else if condition see we are going to copy paste and then you are going to allow your zero so zero is equal to user where he will be redirected to home page okay home page and we'll give his message as logged in success yes uccess successfully so let's check the admin middleware once okay perfect oops we didn't give the path so let us give the path over here to slash home and if he is there we will give here as return and redirect back as simple let's save and now guys let us move again over here to check that okay everything is working fine you can move this to home not on the home page save and now guys let us test this so first let's go to our database and change the role let's move for this way gmail.com we are going to give the role as one one means admin okay zero means user let's go let's try to access with with first admin whether gmail.com one two three four five six seven eight nine click on login okay we have face editing okay auth is not found inside your controller which is login controller so let's move that okay so this is my login controller let's import this class at top so this is the class auth let's minimize and refresh again with gmail.com dot com one two three four five six nine click login so you will be redirected to post okay you can see that welcome admin and yep guys so let's move log out and login as a common user so this is zero rowley status zero and this is home gmail.com so let us try to give here my gmail.com one two three four five six seven eight let's click to login you can see logged in successfully because he's a normal user he redirected to home now let us try to access the post page let's click on it you can see access denied as you are not a admin perfect so guys we have seen how to use the middleware in laravel 8. so guys in this video that's it and let's continue next video with the new topic so guys thank you for watching this video please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 1,339
Rating: undefined out of 5
Keywords: fundaofwebit, how to make middleware in laravel 8, how to give middleware in laravel 8, middleware in laravel, middleware in laravel 8, laravel 8 middleware tutorials, laravel crash course, laravel 8 tutorials, laravel 8 crash course, how to create middleware in laravel 8, laravel 8 middleware, roles and permission in laravel 8
Id: LakUlUTveKk
Channel Id: undefined
Length: 12min 3sec (723 seconds)
Published: Tue May 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.