Skip to content

Commit c61280b

Browse files
committed
fix: handle 401 responses by auto-disconnecting from the connection store
1 parent b78dd47 commit c61280b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/webapp/src/hooks/use-poll-messages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export function usePollMessages() {
120120
signal: AbortSignal.timeout(5_000),
121121
})
122122
if (!response.ok) {
123+
if (response.status === 401) {
124+
useConnectionStore.getState().disconnect()
125+
}
123126
return
124127
}
125128
const payload = (await response.json()) as {

packages/webapp/src/stores/connection.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,20 @@ export const useConnectionStore = create<ConnectionStore>()(
6868
const tempSdk = new TelagentSdk({ baseUrl: nodeUrl })
6969
const result = await tempSdk.unlockSession({ passphrase })
7070

71-
// 3. Create authenticated SDK with session token
71+
// 3. Create authenticated SDK with session token.
72+
// Wrap fetch to detect 401 responses and auto-disconnect so
73+
// ProtectedRoute redirects back to /connect without relying on
74+
// every catch-block to re-throw the error.
7275
const sdk = new TelagentSdk({
7376
baseUrl: nodeUrl,
7477
accessToken: result.sessionToken,
78+
fetchImpl: (input, init) =>
79+
fetch(input, init).then((res) => {
80+
if (res.status === 401) {
81+
get().disconnect()
82+
}
83+
return res
84+
}),
7585
})
7686
set({
7787
nodeUrl,
@@ -119,6 +129,13 @@ export const useConnectionStore = create<ConnectionStore>()(
119129
sdk: new TelagentSdk({
120130
baseUrl: nodeUrl,
121131
accessToken: sessionToken,
132+
fetchImpl: (input, init) =>
133+
fetch(input, init).then((res) => {
134+
if (res.status === 401) {
135+
get().disconnect()
136+
}
137+
return res
138+
}),
122139
}),
123140
status: "connected",
124141
error: undefined,

0 commit comments

Comments
 (0)