Skip to content

fix(auth): log out on 401 responses - #190

Open
fernandatoledo wants to merge 3 commits into
masterfrom
fix/401-logout
Open

fernandatoledo wants to merge 3 commits into
masterfrom
fix/401-logout

Conversation

@fernandatoledo

Copy link
Copy Markdown
Collaborator

What does this do?

Fixes #185. A 401 response now logs the user out.

  • AuthProvider checked for a 401 in the success handler of its response interceptor. Axios rejects non-2xx responses, so that check never ran. The 401 is now handled in the rejection handler, which calls logout() and still rethrows the error.
  • The effect cleanup called client.interceptors.request.eject() with the id of a response interceptor, so the provider's interceptor was never removed. Every remount (StrictMode, Fast Refresh) stacked another copy, and with the 401 handling working each copy would call logout(). It now ejects from response.

Why did you do this?

When the backend revokes a session (password change, logout from another device, server-side expiry) while the stored expiry is still in the future, the app kept treating the user as logged in: isAuthenticated stayed true and protected screens stayed reachable while their API calls failed. With #183, the stored session also decides whether a deep link opens a protected screen directly.

Who/what does this impact?

Any 401 from the shared Axios client now clears the session and sends the user to sign-in. A 401 from the sign-in endpoint (wrong credentials) also calls logout(), which is harmless since there is no session yet. Token refresh is still a TODO; this only covers logging out.

Risk: risk:high (auth).

How did you test this?

  • Added tests

New cases in __tests__/components/providers/auth.test.tsx: a successful response refreshes auth state from storage and is returned unchanged; a 401 logs out and rethrows; a 500 and a non-Axios error keep the session and rethrow; unmount ejects the response interceptor (not a request one). The 401 and eject tests fail on the previous code. pnpm run type-check, pnpm run lint and the full Jest suite pass.

Notes:

Also adds .claude/runs/ to the ESLint ignores, next to the existing .claude/worktrees/. Local agent run output there (Maestro YAML flows) made eslint . fail, which blocked Pushgate's pre-push lint even though those files are not tracked.

Follow-up, not changed here: logout() clears the provider's MMKV storage and state but not the Zustand store in src/lib/auth, whose token src/api/common/interceptors.ts keeps attaching to requests. The Settings logout has the same gap.

🤖 Generated with Claude Code

Handle the 401 in the response interceptor's rejection handler, where
Axios delivers non-2xx responses, and eject the provider's response
interceptor on unmount instead of a request interceptor id.

Fixes #185
@fernandatoledo
fernandatoledo requested a review from a team as a code owner December 3, 2026 13:11
@fernandatoledo fernandatoledo added the risk:high Auth, payments, migrations, security-sensitive code. label Dec 3, 2026
Copilot AI balanced review requested due to automatic review settings December 3, 2026 13:11
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

💯 Test Coverage

Lines Statements Branches Functions
Coverage: 66%
66.26% (497/750) 67.24% (232/345) 61.51% (187/304)

😎 Tests Results

Tests Skipped Failures Errors Time
242 0 💤 0 ❌ 0 🔥 31.832s ⏱️
👀 Tests Details • (66%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files66.2667.2461.5166.62 
report-only-changed-files is enabled. No files were changed in this commit :)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the changes to log users out on shared Axios client 401 responses.

No open issues 🎉

What changed in this PR

Overall Review: ai:clean

Fixes #185 by making the shared Axios client’s 401 responses log users out through AuthProvider.

Changes:

  • Handles 401 errors in the rejection handler while preserving error propagation.
  • Corrects response-interceptor cleanup and adds regression tests.
  • Excludes local .claude/runs/ output from ESLint.
File Description
src/​components/​providers/​auth.tsx Fixes 401 logout handling and interceptor cleanup.
eslint.config.mjs Ignores local agent run output.
__tests__/​components/​providers/​auth.test.tsx Tests response handling, session state, error propagation, and cleanup.

Review effort: Balanced

@label-pr-severity-rs label-pr-severity-rs Bot added the ai:clean No significant issues label Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:clean No significant issues risk:high Auth, payments, migrations, security-sensitive code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

401 responses never log the user out

3 participants