From 9bc2d65b431523ab23140de102a4885088a0772b Mon Sep 17 00:00:00 2001 From: emil Date: Fri, 1 May 2026 16:56:38 +0300 Subject: [PATCH] fix: use hard redirect after login to avoid stale router cache router.push() can reuse a cached middleware redirect that was issued before the cookie was set, sending the user back to /login. window.location.href forces a fresh browser request that carries the newly set access_token cookie. Also changed SameSite=Strict -> SameSite=Lax so the cookie is included in all same-site navigation patterns. Co-Authored-By: Claude Sonnet 4.6 --- frontend/app/(auth)/login/page.tsx | 6 +++--- frontend/lib/store.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/frontend/app/(auth)/login/page.tsx b/frontend/app/(auth)/login/page.tsx index 0f7fbc1..f9b6a60 100644 --- a/frontend/app/(auth)/login/page.tsx +++ b/frontend/app/(auth)/login/page.tsx @@ -1,7 +1,6 @@ 'use client'; import { useState, type FormEvent } from 'react'; -import { useRouter } from 'next/navigation'; import { Button, Input, Card } from '@/components/ui'; import { api } from '@/lib/api'; import { useAuthStore } from '@/lib/store'; @@ -29,7 +28,6 @@ const ROLE_REDIRECT: Record = { }; export default function LoginPage() { - const router = useRouter(); const { setTokens, setUser } = useAuthStore(); const [email, setEmail] = useState(''); @@ -52,7 +50,9 @@ export default function LoginPage() { if (!role) throw new Error('Не удалось определить роль пользователя'); setUser({ ...profile, role }); - router.push(ROLE_REDIRECT[role]); + // Hard redirect so the browser sends a fresh request with the new cookie. + // router.push() can use a stale Next.js router cache and miss the cookie. + window.location.href = ROLE_REDIRECT[role]; } catch (e) { setError(e instanceof Error ? e.message : 'Ошибка входа'); } finally { diff --git a/frontend/lib/store.ts b/frontend/lib/store.ts index 7754982..e6ee455 100644 --- a/frontend/lib/store.ts +++ b/frontend/lib/store.ts @@ -22,7 +22,7 @@ function persistToken(accessToken: string): void { if (typeof window === 'undefined') return; localStorage.setItem('access_token', accessToken); // 15 min lifetime matches the default JWT_ACCESS_TTL - document.cookie = `access_token=${accessToken}; path=/; SameSite=Strict; max-age=900`; + document.cookie = `access_token=${accessToken}; path=/; SameSite=Lax; max-age=900`; } export const useAuthStore = create()(