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('/');}
Source: stackoverflow.com
Answered By: Claudio King
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:
phpuse 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.
Comments
Post a Comment