fix(desktop): prevent auth redirect on transient network errors#1164
fix(desktop): prevent auth redirect on transient network errors#1164AviPeltz wants to merge 2 commits into
Conversation
When the network drops temporarily, Better Auth's useSession() returns isPending=false with session=undefined and an error. Previously, this would redirect authenticated users to /sign-in, causing a jarring flash. This fix: - Tracks if the user was ever authenticated via wasAuthenticatedRef - Uses the error state from useSession() to detect transient failures - Preserves the authenticated UI during transient errors instead of redirecting, allowing the session to recover on the next successful fetch Only redirects to /sign-in when we're confident the user is signed out: no pending, no error, and no session.
📝 WalkthroughWalkthroughAdds session error handling that tracks prior sign-in state and preserves the authenticated UI on transient network errors instead of redirecting to sign-in; introduces Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 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 |
Improves the auth error handling to properly distinguish between: - Network errors (status 0 or undefined) - preserve authenticated UI - Auth errors (401) - redirect to sign-in immediately This ensures that when a session is actually invalid (expired token, revoked session), users are properly redirected to sign-in rather than having the UI preserved incorrectly. Error handling logic: - isNetworkError: status === 0 || status === undefined - isAuthError: status === 401
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
errorstate fromuseSession()to detect network failuresProblem
When the network drops temporarily, Better Auth's
useSession()returns:isPending: falsedata: undefined(no session)error: <network error>The previous code only checked
isPendingandsession, so transient errors looked identical to "signed out", causing an incorrect redirect to/sign-in.Solution
truefalsefalseundefinedfalseundefined/sign-inTest plan
Summary by CodeRabbit