Skip to content

Commit 3a2a7bd

Browse files
jsell-rhclaude
andcommitted
fix(ambient-ui): auto-redirect to SSO login on expired session
Global QueryCache/MutationCache error handler detects 401 responses and redirects to /api/auth/sso/login with returnTo preserving the current URL. Prevents users from seeing stale 401 errors after token expiry — they get seamlessly re-authenticated instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 886633c commit 3a2a7bd

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

components/ambient-ui/src/lib/query-client.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,36 @@ const queryConfig: DefaultOptions = {
1414
},
1515
}
1616

17+
function isAuthError(error: unknown): boolean {
18+
if (error instanceof Error && error.message.includes('401')) return true
19+
if (typeof error === 'object' && error !== null && 'status' in error) {
20+
return (error as { status: number }).status === 401
21+
}
22+
return false
23+
}
24+
25+
let redirecting = false
26+
27+
function handleAuthError() {
28+
if (redirecting || typeof window === 'undefined') return
29+
redirecting = true
30+
const returnTo = encodeURIComponent(window.location.pathname + window.location.search)
31+
window.location.href = `/api/auth/sso/login?returnTo=${returnTo}`
32+
}
33+
1734
export function makeQueryClient() {
1835
return new QueryClient({
1936
defaultOptions: queryConfig,
20-
queryCache: new QueryCache(),
21-
mutationCache: new MutationCache(),
37+
queryCache: new QueryCache({
38+
onError: (error) => {
39+
if (isAuthError(error)) handleAuthError()
40+
},
41+
}),
42+
mutationCache: new MutationCache({
43+
onError: (error) => {
44+
if (isAuthError(error)) handleAuthError()
45+
},
46+
}),
2247
})
2348
}
2449

0 commit comments

Comments
 (0)