fix: auth cookies cross-origin, async params, CSRF/web-vitals errors

- Set SameSite=lax for auth & CSRF cookies in development (cross-port)
- Set refresh_token cookie path to / (was /auth, preventing cross-port send)
- Await params in Next.js 15 async server components (layout, listings, agents)
- Add CSRF token to web-vitals POST requests
- Fix: 401 Unauthorized on all authenticated API calls from web app
- Fix: CSRF token missing on POST requests from different port
- Fix: params.locale sync access warning in generateMetadata

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ho Ngoc Hai
2026-04-13 11:24:45 +07:00
parent a9fa214544
commit 1ebdc5f0b3
6 changed files with 38 additions and 17 deletions

View File

@@ -46,6 +46,12 @@ let flushTimer: ReturnType<typeof setTimeout> | null = null;
const FLUSH_INTERVAL_MS = 5000;
const MAX_BATCH_SIZE = 10;
function getCsrfToken(): string | undefined {
if (typeof document === 'undefined') return undefined;
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]*)/);
return match?.[1] ? decodeURIComponent(match[1]) : undefined;
}
function flushQueue(): void {
if (queue.length === 0) return;
@@ -65,9 +71,14 @@ function flushQueue(): void {
}
function sendViaFetch(body: string): void {
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
const csrfToken = getCsrfToken();
if (csrfToken) headers['X-CSRF-Token'] = csrfToken;
fetch(`${API_BASE_URL}/web-vitals`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers,
credentials: 'include',
body,
keepalive: true,
}).catch(() => {