logout(); } } Auth::guard($newGuard)->login($profile); session(['active_guard' => $newGuard]); } /** * Logs out users from all non-web authentication guards ('admin', 'bank', 'organization') * and sets the session 'active_guard' to 'web'. * * This method is useful for ensuring that only the 'web' guard remains active, * preventing conflicts when switching between different user roles. * * @return void */ public function logoutNonWebGuards() { $presenceService = app(\App\Services\PresenceService::class); foreach (['admin', 'bank', 'organization'] as $guard) { // Set the user offline before logging out if (Auth::guard($guard)->check()) { $user = Auth::guard($guard)->user(); $presenceService->setUserOffline($user, $guard); // Explicitly clear all caches for this user/guard combination \Cache::forget("presence_{$guard}_{$user->id}"); \Cache::forget("presence_last_update_{$guard}_{$user->id}"); } // Clear the guard's online users cache \Cache::forget("online_users_{$guard}_" . \App\Services\PresenceService::ONLINE_THRESHOLD_MINUTES); Auth::guard($guard)->logout(); } session(['active_guard' => 'web']); } }