fix(api): handle 401 responses by redirecting to login

This commit is contained in:
Nils-Johan Gynther
2026-04-17 20:26:28 +02:00
parent 5179e5c5be
commit 8b9ec31dee
+4
View File
@@ -1,3 +1,4 @@
import { redirect } from 'next/navigation';
import { getAuthHeaders } from './auth-headers'; import { getAuthHeaders } from './auth-headers';
const API_BASE = const API_BASE =
@@ -23,6 +24,9 @@ export async function fetchJson<T>(path: string, init?: RequestInit): Promise<T>
}); });
if (!res.ok) { if (!res.ok) {
if (res.status === 401 && isServer) {
redirect('/login');
}
const text = await res.text(); const text = await res.text();
throw new Error(`API ${res.status}: ${text}`); throw new Error(`API ${res.status}: ${text}`);
} }