Laravel Suspend User | Ban User in Laravel | Disable use in laravel | Laravel user login disable

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to tech tool india today i am going to explain about how we can ban or suspend a user in liable user management system so that user can't login in a reliable system so as we already covered with the user management so we this is the list of users which we have we have created some test users so if you have not watched the video how we have created this beautiful admin panel with the user management you can click on the i button and view that video for today let's get started the very first thing which we look is we have name email and these action night buttons what we can start with we have we can add a status column in users table just to make sure we are managing the status of the user who is able to log in and all other who is banned on this laravel application so for let's go into the database c as you can see we have these fields available in the database name email email verified password remember token what we have to do we have to simply add a status column here which will be a by default one which is one for the active and zero for the band users so what we can do we can simply go into the code create a migration just from run a command phprt should make my creation add status column in users table just give a name to this vibration as descriptive as possible it will create a migration for users table migration as you see this schema is created so what we have to add we have to add a tiny integer name it as a status default value would be 1 and this should go after [Music] password so this should go after the password and that's it so in up function we write this status column and in down what we have to do we just drop the status column table drop column we have drop column status that's it so we have written down our migration just run the command now php artisan migrate and it will migrate our this status column in users table let's refresh this table yes we got the status column in the users table and all the status for status for all the users by default is one but next let's open this user model and in the fillable adder status column here just to make sure when we ban or suspend a user it will update the status so that's why we use this fillable property of model eloquent we have added database column status and added a fillable property what next let's go into the as we are using this user's resource to manage the user management what we can do we can add a new route root get you can give it users such update status status uses status and whatever the status code would would be so status code the column name will be this simply user controller class what we can do we can pass inside this and we need to call a function update status in a single quote and that's it one thing which we have to do here is to make a group which have the authenticated middleware so let's make this auth middleware and group it and let's move these urls inside the group so what will do it's always authenticated your authenticated rules this is for user management and this is to update users all right so we have updated this let's create this in a user controller so let's open user controller user controller let's go down create a function it will accept the status it actually should accept two parameter the first one which would be the user id user id and the second one would be status uh code what the status quo so it will accept two parameter one is a user id and the second one is status code just to explain this we have to write to update status of user it will accept parameter as integer user id and the same thing you accept the status code as well success response all right we will put it into try catch block and the cats will throw this let's update the user user where i t is equals to user id [Music] status dollar status code let's take it and update user variable if update user is there what we can do we can return is redirect to root user start index with success uses status updated successfully all right we have done this else what we can return we have to return an error message error failed to update user status fail to take user status all right we are all set with this update status function next we'll open the list uses list and as we have this button there what we can do we can simply add a new button here give it a pan class and instead of redirecting it to user started it let's give it a name here let's give it a name what we can call views is start status dot update we can call this root name here we will pass the first parameter as the user id and second parameter would be on the condition if dollar user pointing to status is equals to one else and and if so this would be a danger as it's one so we have to pass the status code as zero and instead of user let's pass it user id copy the same button paste it here give it a class success instead of zero status quote pass one instead of band pass check all right i think we all are set let's go into the users table and next function what we are selecting yes we are not selecting a status here so let's add a status here and refresh this yes we are getting this let's ban a user yep let's see if this user has updated the status test one two three four five six at test.com so here is the user let's refresh the table yes you see the status has become zero here all right so how we can let's activate it yeah it's working fine now the feature comes like how we can ban this user from login so for that what we have to do we first we need to create a middleware for this to create a middleware what we'll do we'll just ah run a command php artisan make middleware and check that is the middleware name which we are creating it has created inside the middleware this is the function so what we have to check here in the middleware if user is authenticated the very first thing which we have to check is like user is authenticated and second what we have to check art of user status is equal if state is equal to 0 then we have to log out our user then we have to log out this and what we have other things we do we will invalidate the session we'll invalidate this session and we'll regenerate the token we will use this session regenerate open and then use it [Music] login with error message your account is suspended please contact admin so what will what this middleware do is just check if the user authenticated and status is zero so what we'll do is it's all log out this current user and then invalidate the station and regenerate it open and redirect to the login screen with their error message all right to register this middleware what we have to do we just go into the http kernel.php so as we need to activate this checkband for all web roots so what we can do here we simply add a class inside a web middleware so what we'll do app http middleware and check band class that's it so it will check for all the authenticated users let's go into all right we are all set let's go into this users this copy this password for this user is password so nothing to worry about that copy that just log out from here go to the login paste this and password will be password try logging in we have invalidate call a member function invalidate on a null so we have something there to check in code okay so this should be a function model let's refresh this password we'll add a password here and try logging in we have redirected here what we have to do we have to display the error message here so what we can do we can go to the login screen of views auth login.played what we can do we can simply display the login errors if there is a session of error message what we can do here is just create a span give it a class danger text danger and inside this we have to print the session of and let's go to the login screen refresh this what we have try login your account is suspended please contact your admin if i go to this go to the user list and activate this user log out with this user and if i try login this user if i log in yeah it's working so our user suspension is working properly if you found this video informative please do like share and comment on this video if you have any doubt about laravel about this middleware banning any user in level you can comment out to this video i'll come i'll reach out to you and try to resolve any issue which you are facing and in the coming videos i am going to tell you more advanced feature about the laravel please do subscribe for any latest video keep learning stay tuned thank you for [Music] watching [Music] you
Info
Channel: TechTool India
Views: 855
Rating: undefined out of 5
Keywords: laravel, laravel tutorial, laravel 8, laravel for beginners, laravel user login disable, disable user in laravel, laravel 6, laravel 7, how to disable user in laravel, laravel 8 tutorial, laravel tutorial for beginners, user login laravel, laravel authentication, laravel 5, laravel auth, laravel login, laravel 9, laravel 5.3, laravel tutorials, laravel crash course, laravel ban user, laravel users, laravel middleware, laravel auth admin and user, ban user in laravel
Id: MY6BcDW8zwo
Channel Id: undefined
Length: 20min 28sec (1228 seconds)
Published: Tue Jun 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.