Repository navigation
fix (desktop): first pass auth flash fix - #1047
Conversation
📝 WalkthroughWalkthroughOn mount, AuthProvider checks a stored token's expiry and, if valid, sets it in memory and starts a non-blocking background session refetch. The authenticated layout uses session pending, local-token presence, and online status to choose between spinner, sign-in redirect, or an offline retry UI. Changes
Sequence Diagram(s)sequenceDiagram
participant Renderer as Renderer (UI)
participant AuthProv as AuthProvider
participant Storage as LocalStorage
participant API as Auth API
Renderer->>AuthProv: mount
AuthProv->>Storage: read storedToken
alt storedToken exists && not expired
AuthProv->>AuthProv: set token in memory
AuthProv->>API: refetchSession() (async, non-blocking)
API-->>AuthProv: session data / error
AuthProv-->>Renderer: session state updated
else no token or expired
AuthProv-->>Renderer: no token (render sign-in path)
end
Renderer->>Renderer: layout checks isPending, hasLocalToken, isOnline
alt isPending && hasLocalToken
Renderer-->>Renderer: render full-screen Spinner
else isPending && !hasLocalToken
Renderer-->>Renderer: navigate to /sign-in
else !signedIn && hasLocalToken && !isOnline
Renderer-->>Renderer: render offline retry UI (Retry -> triggers refetch)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@apps/desktop/src/renderer/providers/AuthProvider/AuthProvider.tsx`:
- Around line 32-43: The code currently treats storedToken.expiresAt by
comparing new Date(storedToken.expiresAt) < new Date(), which accepts invalid
dates and lets exact-expiry tokens through; update the guard around
storedToken.expiresAt in the AuthProvider logic to first parse and validate the
timestamp (e.g., Date.parse or new Date(...) and check isNaN) and then use a <=
comparison against now to treat exact-expiry as expired; if the parsed date is
invalid or <= now, do NOT call setAuthToken(storedToken.token) or
refetchSession(), otherwise proceed to setAuthToken and kick off
refetchSession() as before.
| // If token exists and isn't expired locally, set it and validate in background | ||
| if (storedToken?.token && storedToken?.expiresAt) { | ||
| setAuthToken(storedToken.token); | ||
| refetchSession(); | ||
| const isExpired = new Date(storedToken.expiresAt) < new Date(); | ||
|
|
||
| if (!isExpired) { | ||
| setAuthToken(storedToken.token); | ||
| // Validate session in background - don't block UI on network | ||
| refetchSession().catch((err) => { | ||
| console.warn("[auth] Background session validation failed:", err); | ||
| }); | ||
| } | ||
| // If expired, don't set token - user will be redirected to sign-in |
There was a problem hiding this comment.
Guard against invalid or boundary expiresAt values.
new Date(expiresAt) < new Date() treats invalid timestamps as “not expired” and lets exact-expiry tokens through. Prefer isNaN guard and <= to avoid accepting malformed/just-expired tokens.
💡 Suggested fix
- const isExpired = new Date(storedToken.expiresAt) < new Date();
+ const expiresAtMs = new Date(storedToken.expiresAt).getTime();
+ const nowMs = Date.now();
+ const isExpired = Number.isNaN(expiresAtMs) || expiresAtMs <= nowMs;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // If token exists and isn't expired locally, set it and validate in background | |
| if (storedToken?.token && storedToken?.expiresAt) { | |
| setAuthToken(storedToken.token); | |
| refetchSession(); | |
| const isExpired = new Date(storedToken.expiresAt) < new Date(); | |
| if (!isExpired) { | |
| setAuthToken(storedToken.token); | |
| // Validate session in background - don't block UI on network | |
| refetchSession().catch((err) => { | |
| console.warn("[auth] Background session validation failed:", err); | |
| }); | |
| } | |
| // If expired, don't set token - user will be redirected to sign-in | |
| // If token exists and isn't expired locally, set it and validate in background | |
| if (storedToken?.token && storedToken?.expiresAt) { | |
| const expiresAtMs = new Date(storedToken.expiresAt).getTime(); | |
| const nowMs = Date.now(); | |
| const isExpired = Number.isNaN(expiresAtMs) || expiresAtMs <= nowMs; | |
| if (!isExpired) { | |
| setAuthToken(storedToken.token); | |
| // Validate session in background - don't block UI on network | |
| refetchSession().catch((err) => { | |
| console.warn("[auth] Background session validation failed:", err); | |
| }); | |
| } | |
| // If expired, don't set token - user will be redirected to sign-in |
🤖 Prompt for AI Agents
In `@apps/desktop/src/renderer/providers/AuthProvider/AuthProvider.tsx` around
lines 32 - 43, The code currently treats storedToken.expiresAt by comparing new
Date(storedToken.expiresAt) < new Date(), which accepts invalid dates and lets
exact-expiry tokens through; update the guard around storedToken.expiresAt in
the AuthProvider logic to first parse and validate the timestamp (e.g.,
Date.parse or new Date(...) and check isNaN) and then use a <= comparison
against now to treat exact-expiry as expired; if the parsed date is invalid or
<= now, do NOT call setAuthToken(storedToken.token) or refetchSession(),
otherwise proceed to setAuthToken and kick off refetchSession() as before.
- Remove verbose comments and console.warn from AuthProvider - Add offline screen when disconnected with valid token - Add offline indicator badge in TopBar - Extract useOnlineStatus hook for shared use
# Conflicts: # apps/desktop/src/renderer/routes/_authenticated/layout.tsx
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Description
Related Issues
Type of Change
Testing
Screenshots (if applicable)
Additional Notes
Summary by CodeRabbit
New Features
Bug Fixes