microsoft_id) { $lastChecked = session('ms_session_last_checked'); // Check every 5 seconds upon page access/refresh if (!$lastChecked || now()->diffInSeconds($lastChecked) > 5) { // Set timestamp immediately to prevent redirect loop session(['ms_session_last_checked' => now()]); // Store target URL to return to after silent verification session(['sso_check_origin' => $request->fullUrl()]); // Determine domain hint based on user's email domain $domainHint = (str_ends_with($user->email, '.onmicrosoft.com') || (str_contains($user->email, '@') && !str_ends_with($user->email, 'outlook.com') && !str_ends_with($user->email, 'hotmail.com') && !str_ends_with($user->email, 'live.com'))) ? 'organizations' : 'consumers'; try { // Redirect to Microsoft with prompt=none to check session status return Socialite::driver('microsoft') ->redirectUrl(route('sso.callback')) ->with([ 'login_hint' => $user->email, 'domain_hint' => $domainHint, 'prompt' => 'none' ]) ->redirect(); } catch (\Exception $e) { \Illuminate\Support\Facades\Log::error('Microsoft session silent verification failed', [ 'message' => $e->getMessage() ]); } } } return $next($request); } }