Skip to content

fix(auth): retry user initialization before clearing session#5814

Open
rogeecn wants to merge 1 commit intousememos:mainfrom
rogeecn:fix/auth-init-retry
Open

fix(auth): retry user initialization before clearing session#5814
rogeecn wants to merge 1 commit intousememos:mainfrom
rogeecn:fix/auth-init-retry

Conversation

@rogeecn
Copy link
Copy Markdown

@rogeecn rogeecn commented Apr 7, 2026

Summary

  • retry auth initialization requests with exponential backoff before treating the session as lost
  • keep post-login transient failures from immediately clearing frontend user state
  • add a regression test covering retry success and retry exhaustion paths

Test Plan

  • node --test web/test/auth-initialize.test.mjs
  • pnpm lint

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced authentication initialization resilience with automatic retry capability to improve reliability during transient connection issues
  • Tests

    • Added comprehensive test coverage for authentication retry behavior and failure scenarios

@rogeecn rogeecn requested a review from a team as a code owner April 7, 2026 03:31
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 7, 2026

📝 Walkthrough

Walkthrough

A new retry utility for authentication initialization with exponential backoff is introduced and integrated into AuthContext, wrapping the getCurrentUser and fetchUserSettings sequence with automatic retry logic (maximum 3 retries, starting 500ms delay).

Changes

Cohort / File(s) Summary
Retry Utility Implementation
web/src/contexts/auth-initialize.ts
New module exporting retryAuthInitialization<T> function that wraps async operations with exponential backoff retry logic (500ms initial delay, doubling per attempt, max 3 retries before error propagation).
AuthContext Integration
web/src/contexts/AuthContext.tsx
Modified initialization sequence to wrap getCurrentUser + fetchUserSettings calls within retryAuthInitialization, with consolidated error handling via `if (!currentUser
Test Coverage
web/test/auth-initialize.test.mjs
Two test cases verifying retry success behavior (4 attempts, exponential delays [500, 1000, 2000]ms) and failure behavior (error rejection after max retries with same delay sequence).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • memoclaw

Poem

🐰 A retry, a wait, exponential cheer,
Auth flows with courage through delay and fear,
Backoff in doubles, three chances to try,
Authentication hops ever up to the sky!

🚥 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
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: implementing retry logic for user initialization before clearing the session, which aligns with the primary objective of preventing transient failures from clearing frontend state.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3820a8c523

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +21 to +25
} catch (error) {
if (attempt >= AUTH_INIT_MAX_RETRIES) {
throw error;
}
await sleep(delayMs);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid retrying terminal unauthenticated failures

retryAuthInitialization() retries every thrown error, including ConnectError unauthenticated cases that are already handled in the auth interceptor (shouldHandleUnauthenticatedRetry in web/src/connect.ts) and indicate the session is gone. In that path, this loop adds 500ms+1000ms+2000ms delay before clearing state, so users with revoked/invalid sessions wait ~3.5s longer to reach signed-out UI (or redirect), which is a regression from the previous immediate fallback behavior.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
web/src/contexts/AuthContext.tsx (1)

8-8: Use @/ alias for consistency with other imports.

As per coding guidelines, use the @/ alias for absolute imports in the frontend instead of relative paths.

♻️ Proposed fix
-import { retryAuthInitialization } from "./auth-initialize";
+import { retryAuthInitialization } from "@/contexts/auth-initialize";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/src/contexts/AuthContext.tsx` at line 8, Replace the relative import of
retryAuthInitialization in AuthContext.tsx with the project alias form; change
the import path from "./auth-initialize" to use the "@/..." alias (e.g.
"@/contexts/auth-initialize") so the statement importing retryAuthInitialization
uses the alias-consistent path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@web/src/contexts/AuthContext.tsx`:
- Line 8: Replace the relative import of retryAuthInitialization in
AuthContext.tsx with the project alias form; change the import path from
"./auth-initialize" to use the "@/..." alias (e.g. "@/contexts/auth-initialize")
so the statement importing retryAuthInitialization uses the alias-consistent
path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4c54f2c1-cff1-4e35-a8fc-26782d86220a

📥 Commits

Reviewing files that changed from the base of the PR and between 61c9638 and 3820a8c.

📒 Files selected for processing (3)
  • web/src/contexts/AuthContext.tsx
  • web/src/contexts/auth-initialize.ts
  • web/test/auth-initialize.test.mjs

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