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:
jtricerolph 2026-07-12 20:09:17 +00:00
parent d25e3421a8
commit 7c8991d5ce

View file

@ -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}`)