React authentication¶
Client-side authentication and authorization for the React SPA.
Related: System Architecture — Authentication flow, Auth API, Security Features.
Flow (client)¶
- Login — form posts credentials to
POST /api/v1/auth/login(with CSRF). - Token storage
- Access token → Redux / memory only (not
localStorage) - Refresh token → HttpOnly cookie set by the backend (not readable by JS)
- Session restore hint → non-secret
localStorageflag so a reload, a new tab, or a browser restart can attempt cookie refresh - Session restore → on load,
InitAuth(App.tsx) starts therestoreSessionthunk, and nothing else starts it. When the hint exists, the thunk refreshes the access token and then fetchesGET /users/me.SessionGateholds the routes behind a loading screen until the restore settles, whether it succeeded or failed. - Authenticated requests — Axios client attaches
Authorization: Bearer <access_token>and sends cookies (withCredentials: true). - Refresh — on HTTP 401 (when a session hint exists), interceptor calls
POST /auth/new_access_tokenwith CSRF; cookie is sent automatically; retries the original request or logs out. A 401 from the refresh endpoint itself is excluded from this path so it cannot recurse. Refreshes that start while one is in flight share that request, and requests waiting on a CSRF token share one fetch, the retry after a CSRF refusal included. Parallel fetches or refreshes would otherwise fail each other's CSRF check. - Temporary or expired password — a 403 whose
detail.codeispassword_change_requiredmeans an administrator set the user's password, or it has expired (REQUIRE_PASSWORD_CHANGE_DAYS). The interceptor marks the stored userneeds_to_change_passwordand does not log out;ProtectedRoutethen sends the user to/change-password-required. That page (underAuthLayout) offers only its own set-a-new-password form and Sign out. It does not renderChangePasswordForm. Its copy says the password expired whenpassword_expires_athas passed, and names a temporary password otherwise. A successful change stores the user and access token from the response and goes to/dashboard. See ADR 0014 and ADR 0016. - Logout — Both sign-out actions live only in the header user menu. Sign out calls
POST /auth/logout, which ends this session only (allowlist entries for that session + cookie cleared server-side) and clears client memory/hint. Other devices stay signed in. Sign out everywhere asks first ("You’ll be signed out on this device too."), then postsPOST /auth/logout/all, does the same local cleanup and sends the user to Sign in, which shows "You’ve been signed out everywhere." Cancelling the confirmation leaves the current session alone.
Backend session invalidation uses a Redis allowlist (app/utils/allowlist.py, behind app/utils/sessions.py), not a JWT jti blacklist. See ADR 0001 and ADR 0006.
CSRF¶
State-changing auth calls (login, refresh, logout, logout everywhere, password change, etc.) require CSRF. Obtain/attach tokens via the auth/CSRF service layer; see Security Features and react-frontend CSRF-related services/tests.
Route and UI guards¶
- Route level —
ProtectedRoute(and layout wrappers) require authentication and optionally roles/permissions. A user withneeds_to_change_passwordis redirected to/change-password-requiredfrom every protected route. - Component level —
usePermissions/useAuthgate buttons and panels (user.create,role.read, …).
Keep permission names aligned with backend permission records.
Security checklist (frontend)¶
- Do not persist access or refresh tokens in
localStorage. - Rely on backend logout to clear the HttpOnly refresh cookie; clear the session hint on logout / failed refresh.
- Prefer sanitized user input before display; rely on backend validation as the source of truth.
- Treat CORS misconfiguration as an ops issue — see CORS troubleshooting.
Where the code lives¶
| Concern | Typical location |
|---|---|
| Sign in, register, verify email, resend, and password reset UI | src/features/auth/ (LoginPage, SignupPage, VerifyEmailPage, ResendVerificationEmailForm, PasswordResetRequestPage, PasswordResetConfirmPage) |
| Auth Redux slice | src/store/slices/authSlice.ts (logoutUser, logoutAllUser) |
| Change-password form (Security tab of Account settings) | src/components/auth/ChangePasswordForm.tsx, shown by src/features/account/SecurityTab.tsx |
| Temporary-password page | src/features/auth/ChangePasswordRequiredPage.tsx (own form and Sign out) |
| Header user menu (Sign out, Sign out everywhere) and the Security tab of Account settings (Sign out everywhere) | src/components/layout/UserMenu.tsx (SignOutEverywhereDialog, shared with src/features/account/SecurityTab.tsx) |
| Axios + interceptors | src/services/api.ts, authTokenManager.ts |
| Hooks | src/hooks/useAuth.ts, usePermissions.ts |
| Route guards | src/components/auth/ / src/components/layout/ |