Hello Laravel friends.
I have a small request from a customer, to log out users from a website when they log in from different browsers or pcs. I saw that there was a logoutOtherDevices method in Laravel so I decided to use it.
I overloaded the login method in my LoginController, as:
protected function authenticated(Request $request)
{
Auth::guard(get_guard())->logoutOtherDevices($request->password);
}
I uncommented the AuthenticateSession line in the Kernel.php:
protected $middlewareGroups = [
'web' => [
...
\Illuminate\Session\Middleware\AuthenticateSession::class,
...
],
...
];
The problem is , when I debug this code, I find that inside the call to logoutOtherDevices method in SessionGuard, the user is null, so it always returns without resetting the hash.
public function logoutOtherDevices($password, $attribute = 'password')
{
if (! $this->user()) {
return; // execution always comes here when I log-in
}
...
I know we have more than one guard, so it might be an issue with the route and the middlewares applied. I have been using Laravel for a few months so I don't understand the entire logic of the middlewares and the guards. Can someone help me resolve this issue? Here is my auth config:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'staff' => [
'driver' => 'session',
'provider' => 'staff'
],
'partner' => [
'driver' => 'session',
'provider' => 'partner'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'partner' => [
'driver' => 'eloquent',
'model' => \App\Models\Partner::class,
],
'staff' => [
'driver' => 'eloquent',
'model' => \App\Models\Staff::class
],
'users' => [
'driver' => 'database',
'table' => 'users',
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'partner' => [
'provider' => 'partner',
'table' => 'password_resets',
'expire' => 60,
],
'staff' => [
'provider' => 'staff',
'table' => 'password_resets',
'expire' => 60,
]
],
];
Why is it that I'm not able to fetch the user from the Session Guard?
Thanks for your help