Fix expired session showing EmbeddedFallback instead of login page
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0511ac8d82
commit
803332e6a3
2 changed files with 7 additions and 13 deletions
|
|
@ -8,6 +8,10 @@ async function request<T>(path: string, opts: RequestInit = {}): Promise<T> {
|
||||||
headers: { 'Content-Type': 'application/json', ...opts.headers },
|
headers: { 'Content-Type': 'application/json', ...opts.headers },
|
||||||
...opts,
|
...opts,
|
||||||
})
|
})
|
||||||
|
if (res.status === 401) {
|
||||||
|
;(window.top ?? window).location.href = '/login'
|
||||||
|
throw new Error('Unauthenticated')
|
||||||
|
}
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const err = await res.json().catch(() => ({ error: res.statusText }))
|
const err = await res.json().catch(() => ({ error: res.statusText }))
|
||||||
throw new Error((err as { error?: string }).error || `Request failed: ${res.status}`)
|
throw new Error((err as { error?: string }).error || `Request failed: ${res.status}`)
|
||||||
|
|
|
||||||
|
|
@ -12,30 +12,20 @@ export function useAuth() {
|
||||||
|
|
||||||
export default function AuthGate({ children }: { children: React.ReactNode }) {
|
export default function AuthGate({ children }: { children: React.ReactNode }) {
|
||||||
const [user, setUser] = useState<User | null>(null)
|
const [user, setUser] = useState<User | null>(null)
|
||||||
const [error, setError] = useState<string | null>(null)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch('/reports/api/auth/verify?app=reports', { credentials: 'include' })
|
fetch('/reports/api/auth/verify?app=reports', { credentials: 'include' })
|
||||||
.then(r => {
|
.then(r => {
|
||||||
if (r.status === 401 || r.status === 403) {
|
if (!r.ok) {
|
||||||
window.location.href = `/portal?redirect=${encodeURIComponent(window.location.href)}`
|
;(window.top ?? window).location.href = '/login'
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
if (!r.ok) throw new Error(`Auth check failed: ${r.status}`)
|
|
||||||
return r.json()
|
return r.json()
|
||||||
})
|
})
|
||||||
.then(data => { if (data) setUser(data) })
|
.then(data => { if (data) setUser(data) })
|
||||||
.catch(err => setError(err.message))
|
.catch(() => { ;(window.top ?? window).location.href = '/login' })
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return (
|
|
||||||
<div style={{ padding: 32, color: '#991b1b', fontFamily: 'sans-serif' }}>
|
|
||||||
Authentication error: {error}
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return (
|
return (
|
||||||
<div style={{
|
<div style={{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue