Repository navigation
fix(desktop): improve auth token handling on re-authentication - #1000
Conversation
- Subscription no longer emits on initial subscribe (use getStoredToken query for hydration) - Clear old session state before setting new token on re-authentication - Add documentation for auth events Previously, when a user re-authenticated (e.g., switching accounts), the old session state could persist briefly causing stale data issues. Now the AuthProvider explicitly clears the old session before setting the new token.
📝 WalkthroughWalkthroughThe changes introduce a new Changes
Sequence Diagram(s)sequenceDiagram
participant AP as AuthProvider
participant Sub as onTokenChanged<br/>Subscription
participant AuthClient as AuthClient
AP->>Sub: Subscribe to token changes
Sub-->>AP: Emit token update {token, expiresAt}
rect rgba(100, 150, 200, 0.5)
Note over AP: Clear session state
AP->>AP: setAuthToken(null)
AP->>AuthClient: signOut(fetchOptions)
AuthClient-->>AP: signOut complete
end
rect rgba(150, 200, 100, 0.5)
Note over AP: Apply new token
AP->>AP: setAuthToken(data.token)
end
alt Null token received
Sub-->>AP: Emit null
AP->>AP: setAuthToken(null)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/desktop/src/lib/trpc/routers/auth/index.ts (1)
93-97: Avoid silently swallowing file deletion errors.The
.catch(() => {})on line 94 silently discards any error fromfs.unlink. Per coding guidelines, errors should at minimum be logged with context.Proposed fix
signOut: publicProcedure.mutation(async () => { - await fs.unlink(TOKEN_FILE).catch(() => {}); + await fs.unlink(TOKEN_FILE).catch((err) => { + // ENOENT is expected if already signed out or file never created + if (err.code !== "ENOENT") { + console.error("[auth/signOut] Failed to delete token file:", err); + } + }); authEvents.emit("token-cleared"); return { success: true }; }),
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
Fixes an issue where re-authenticating (e.g., switching accounts) could leave stale session state, causing data inconsistencies.
Changes
getStoredTokenquery for initial hydration instead of relying on subscription's initial emitRoot Cause
The
onTokenChangedsubscription was emitting the current token on subscribe, which worked for initial load but caused issues on re-authentication. When a new OAuth callback came in, the subscription would fire with the new token, but the old session state wasn't cleared first.Test Plan
Summary by CodeRabbit
New Features
Improvements
Removals
✏️ Tip: You can customize this high-level summary in your review settings.