fix(api): handle 401 responses by redirecting to login
This commit is contained in:
@@ -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}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user