Redirect to login on mid-session 401 instead of silently failing
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d25e3421a8
commit
7c8991d5ce
1 changed files with 4 additions and 0 deletions
|
|
@ -4,6 +4,10 @@ const BASE = '/twin-optimiser/api'
|
||||||
|
|
||||||
async function request<T>(path: string, opts?: RequestInit): Promise<T> {
|
async function request<T>(path: string, opts?: RequestInit): Promise<T> {
|
||||||
const res = await fetch(BASE + path, { credentials: 'include', ...opts })
|
const res = await fetch(BASE + path, { credentials: 'include', ...opts })
|
||||||
|
if (res.status === 401) {
|
||||||
|
;(window.top ?? window).location.href = '/login'
|
||||||
|
throw new Error('Unauthenticated')
|
||||||
|
}
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const body = await res.json().catch(() => ({})) as { error?: string }
|
const body = await res.json().catch(() => ({})) as { error?: string }
|
||||||
throw new Error(body.error || `HTTP ${res.status}`)
|
throw new Error(body.error || `HTTP ${res.status}`)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue