-
Notifications
You must be signed in to change notification settings - Fork 4
Add centralized invalid session cleanup and wire Dapper to it #1660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,31 @@ import { | |||||||||||||
|
|
||||||||||||||
| import { DapperTsInterface } from "../index"; | ||||||||||||||
|
|
||||||||||||||
| function isInvalidTokenError(error: ApiError): boolean { | ||||||||||||||
| const detail = extractErrorDetail(error.responseJson); | ||||||||||||||
| return ( | ||||||||||||||
| typeof detail === "string" && detail.toLowerCase().includes("invalid token") | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function extractErrorDetail(payload: unknown): string | undefined { | ||||||||||||||
| if (!payload) { | ||||||||||||||
| return undefined; | ||||||||||||||
| } | ||||||||||||||
| if (typeof payload === "string") { | ||||||||||||||
| return payload; | ||||||||||||||
| } | ||||||||||||||
| if ( | ||||||||||||||
| typeof payload === "object" && | ||||||||||||||
| payload !== null && | ||||||||||||||
|
||||||||||||||
| "detail" in payload && | ||||||||||||||
| typeof (payload as { detail?: unknown }).detail === "string" | ||||||||||||||
| ) { | ||||||||||||||
| return (payload as { detail: string }).detail; | ||||||||||||||
| } | ||||||||||||||
| return undefined; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export async function getCurrentUser(this: DapperTsInterface) { | ||||||||||||||
| try { | ||||||||||||||
| const data = await fetchCurrentUser({ | ||||||||||||||
|
|
@@ -17,13 +42,13 @@ export async function getCurrentUser(this: DapperTsInterface) { | |||||||||||||
| return data; | ||||||||||||||
| } catch (error) { | ||||||||||||||
| if (error instanceof ApiError && error.response.status === 401) { | ||||||||||||||
| // If the user is not authenticated, we remove the session hook | ||||||||||||||
| this.removeSessionHook?.(); | ||||||||||||||
| if (isInvalidTokenError(error)) { | ||||||||||||||
| // If the token is invalid, clear any persisted session data | ||||||||||||||
| this.removeSessionHook?.(); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+45
to
+48
|
||||||||||||||
| if (isInvalidTokenError(error)) { | |
| // If the token is invalid, clear any persisted session data | |
| this.removeSessionHook?.(); | |
| } | |
| // Clear any persisted session data for all 401 errors | |
| this.removeSessionHook?.(); |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,8 @@ export interface ContextInterface { | |||||||||
| clearSession: (clearApiHost?: boolean) => void; | ||||||||||
| /** Remove session cookies. */ | ||||||||||
| clearCookies: (domain: string) => void; | ||||||||||
| /** Clear all persisted session data and flag as stale. */ | ||||||||||
|
||||||||||
| /** Clear all persisted session data and flag as stale. */ | |
| /** | |
| * Clear all persisted session data (current user, API host, cookies) and mark session as stale. | |
| */ |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new clearInvalidSession function calls clearSession(_storage, true), which clears the API host, while the previous implementation at line 205 called clearSession(_storage, false), which preserved the API host. This is a subtle behavior change that could affect session recovery flows.
Verify this is intentional. If the API host should be preserved during invalid session cleanup (to allow the user to re-authenticate with the same API), consider passing false instead.
| clearSession(_storage, true); | |
| clearSession(_storage, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string matching logic using
.includes("invalid token")is fragile and could fail to match:.toLowerCase()helps)Consider using a more robust approach, such as: