Skip to content

fix(desktop): prevent auth redirect on transient network errors#1164

Closed
AviPeltz wants to merge 2 commits into
mainfrom
fix/auth-network-resilience
Closed

fix(desktop): prevent auth redirect on transient network errors#1164
AviPeltz wants to merge 2 commits into
mainfrom
fix/auth-network-resilience

Conversation

@AviPeltz
Copy link
Copy Markdown
Collaborator

@AviPeltz AviPeltz commented Feb 3, 2026

Summary

  • Fixes the auth flash issue where users get redirected to sign-in on temporary network drops
  • Tracks "was ever authenticated" state to distinguish between confirmed sign-out vs transient errors
  • Uses Better Auth's error state from useSession() to detect network failures

Problem

When the network drops temporarily, Better Auth's useSession() returns:

  • isPending: false
  • data: undefined (no session)
  • error: <network error>

The previous code only checked isPending and session, so transient errors looked identical to "signed out", causing an incorrect redirect to /sign-in.

Solution

State isPending session error Action
Loading true - - Render nothing
Authenticated false - Render app
Transient error false undefined Keep authenticated UI
Signed out false undefined - Redirect to /sign-in

Test plan

  • Verify normal sign-in/sign-out flow works correctly
  • Simulate network drop (disable network briefly) while authenticated - should NOT flash sign-in
  • Verify explicit sign-out still redirects to sign-in page
  • Verify initial load without token redirects to sign-in

Summary by CodeRabbit

  • Bug Fixes
    • Improved session error handling: transient network problems no longer force previously signed-in users to the sign-in screen; the app preserves the authenticated UI and avoids unexpected redirects.
    • More robust detection of auth vs network errors to reduce false sign-out behavior.

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.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

Adds 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 wasAuthenticatedRef, adjusts useEffect logic, and logs a warning when a transient error occurs after prior authentication.

Changes

Cohort / File(s) Summary
Session error & auth flow
apps/desktop/src/renderer/routes/_authenticated/layout.tsx
Adds wasAuthenticatedRef to remember prior signed-in state; captures error from authClient.useSession(), distinguishes transient/network errors from auth errors, preserves authenticated UI and logs a warning on transient failures, and retains existing isPending render behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • CharlieHelps

Poem

🐰 I nibbled code where sessions play,
A ref to keep the night at bay,
When networks hiccup and errors cry,
The UI stays — no goodbye,
A logged soft warning, hop and try ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: preventing auth redirects on transient network errors, which matches the core change in the code.
Description check ✅ Passed The description is comprehensive and well-structured, covering the problem, solution with a clear state matrix, and test plan. All template sections are adequately addressed.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/auth-network-resilience

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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
@AviPeltz AviPeltz closed this Feb 4, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 4, 2026

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ⚠️ Neon database branch
  • ⚠️ Electric Fly.io app

Thank you for your contribution! 🎉

@Kitenite Kitenite deleted the fix/auth-network-resilience branch February 7, 2026 02:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant