fix(console): fix stuck authentication check and add reliable redirect to /console/login
- Add immediate redirection to /console/login when unauthenticated in AdminLayout - Add timeout (4000ms) with AbortController in adminApi.ts to prevent infinite pending fetch - Provide explicit sign-in link in layout fallback view
This commit is contained in:
@@ -153,23 +153,32 @@ async function request<T>(
|
||||
|
||||
const baseUrl = getAdminApiBaseUrl();
|
||||
const url = `${baseUrl}${endpoint}`;
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 4000);
|
||||
|
||||
if (response.status === 204) {
|
||||
return {} as T;
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
signal: controller.signal,
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (response.status === 204) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
const json = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const errorMsg = json?.error?.message || `Request failed with status ${response.status}`;
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
return json.data;
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
const json = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
const errorMsg = json?.error?.message || `Request failed with status ${response.status}`;
|
||||
throw new Error(errorMsg);
|
||||
}
|
||||
|
||||
return json.data;
|
||||
}
|
||||
|
||||
// 0. Auth Verification
|
||||
|
||||
Reference in New Issue
Block a user