Laravel 5 Logout Specific User

✔ Recommended Answer

I think that the fastest solution is to add a flag to the users DB table, for example a boolean column to_logout and the in the Auth middleware add something like this code.

$user = Auth::user();if($user->to_logout){    Auth::logout();    $user->update(['to_update' => 0]);    return redirect('/');}


Answered By: Claudio King

Method #2

To log out a specific user in Laravel 5, you can use the logoutOtherDevices method provided by the Auth facade. This method will invalidate all tokens for a user except the current one, effectively logging them out from other devices. Here's an example:

use Illuminate\Support\Facades\Auth; // Get the user you want to log out $user = User::find($userId); // Log out the user from other devices Auth::logoutOtherDevices($user->password);

In this example, $userId is the ID of the user you want to log out. You can use any method to retrieve the user, such as User::findOrFail($userId) or Auth::user(). Once you have the user, you can call the logoutOtherDevices method on the Auth facade, passing the user's password as an argument.

This method will invalidate all tokens for the user except the current one, so the user will be logged out from all other devices.


Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755