get(self::INTEGRATION_SESSION_KEY)) { return $next($request); } $currentSessionId = session()->getId(); $userId = Auth::user()->id; // Invalidate all other sessions, keeping integration sessions. $otherSessions = DB::table('sessions') ->select('id', 'payload') ->where('user_id', $userId) ->where('id', '!=', $currentSessionId) ->get(); foreach ($otherSessions as $otherSession) { if ($this->isIntegrationSession($otherSession->payload)) { continue; } DB::table('sessions')->where('id', $otherSession->id)->delete(); } } return $next($request); } /** * Checks the stored payload for the integration marker. */ protected function isIntegrationSession(?string $payload): bool { if (empty($payload)) { return false; } $decoded = @unserialize(@base64_decode($payload, true), ['allowed_classes' => false]); if (!is_array($decoded)) { $json = json_decode((string) $payload, true); $decoded = is_array($json) ? $json : null; } return is_array($decoded) && !empty($decoded[self::INTEGRATION_SESSION_KEY]); } }