Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danibanai's avatar

Laravel - force logout of specific user by user id

I use laravel 5.2.

I want to know how i can force user to logout by id. I'm building panel admin with the option to dis-active specific user that are currently logged in to the web application

laravel give you the next option for Auth user

Auth::logout()

But i don't want to logout the Auth user. i'm the Auth user. I want to force logout a specific user by is id like when we login user with specific id

Auth::loginUsingId($id);

Their is a option like this?

Auth::logoutUsingId($id);

Thank!!!

0 likes
7 replies
SaeedPrez's avatar

I think the easiest way is to add a column to your users table, for example active and then create a middleware that checks if the user account is active. If it's not active, you log them out and/or redirect them to a page with more info.

I did this in one of my projects, where I would redirect inactive users to a suspended page with some basic information and also contact information.

1 like
Cronix's avatar

Not by itself, but if you have a middleware that is checking for it, then yes you can.

Something like

if (!Auth::user()->isActive()) {
    Auth::logout();
}

or

if (Auth::id() == 1) {
    Auth::logout();
    return redirect('/somewhere/else');
}
1 like
anonymouse703's avatar

@Cronix In my case was i have a Connection Monitoring module to list all the login users, if ever the admin wish to disconnect the user I passed the id through AJAX into my controller then i will logout the specific user... But i will try your logic if it's workings...Thanks.

anonymouse703's avatar

@Cronix i tried the two man and it will just logging out the current user and not the specific user.

Cronix's avatar

Yes, Auth::user() is the current user. I was saying to put it in middleware. Then, when the user with id of 1 (per my example) accesses the app and they are logged in, it will log them out.

https://laravel.com/docs/5.6/middleware

So if you added an is_active column on your users table, the admin can send an ajax request and set that column to a 0 for that user. Then when the user access any route that is using your custom middleware, it would log them out if you had this for your middleware:

if ( ! Auth::user()->is_active) {
    Auth::logout();
    redirect('/somewhere/else');
}

Please or to participate in this conversation.