Login returns 200 but user stays logged out

Reacthighconfidence mediumcomplete

Summary

The API appears to succeed, but client-side session state is never updated — likely a token persistence or auth-state wiring failure after a 200 response.

What went wrong?

Login request succeeds (HTTP 200) while the UI authentication state remains unauthenticated.

Expected: The user should navigate to the home screen after successful authentication.

Root cause analysis

Hypotheses below are evidence-based suggestions — not confirmed facts.

  • Access token not persisted after login

    medium

    If the response body contains a token but the client never writes it to storage (or writes to the wrong key), subsequent auth checks still fail.

    • API returns HTTP 200
    • UI remains on login screen
    • No navigation to home after success
  • Auth context not updated after successful response

    medium

    The login handler may ignore the response payload or fail to call setUser / dispatch(loginSuccess), so React state never flips to authenticated.

    • HTTP 200 observed
    • User still shown as logged out

Event timeline

  1. User clicks Login
  2. Login API request is triggered
  3. API returns HTTP 200
  4. Token storage or auth state update fails (inferred)
  5. Authentication state is not updated (inferred)
  6. Application remains on the login screen

Suggested fix

Persist token and update auth state on 200

After a successful login response, store the token, update auth state, then navigate. Handle storage failures explicitly.

async function onLoginSuccess(res: Response) {
  const data = await res.json();
  if (!res.ok || !data.accessToken) throw new Error("Login failed");
  localStorage.setItem("accessToken", data.accessToken);
  setUser(data.user);
  navigate("/home");
}

Ensures a 200 response always results in persisted credentials and a state change the UI can observe.

Reproduction steps

  1. Open the login screen with DevTools Network tab open.
  2. Submit valid credentials.
  3. Confirm the login request returns HTTP 200.
  4. Observe whether localStorage/sessionStorage gains an access token.
  5. Confirm whether auth state / route changes to the home screen.

Verification checklist

  • After login, accessToken exists in storage.
  • Auth context reports an authenticated user.
  • Protected routes render without redirecting back to login.
  • Refresh keeps the session if that is expected behaviour.

AI coding assistant prompt

Analyze the authentication flow in this project. The login API returns HTTP 200, but the user remains on the login screen. Investigate token persistence, authentication state management and navigation. Use the supplied evidence and suggest a minimal, safe fix.

Redaction summary

Best-effort local redaction matched: Authorization headers would be redacted before any remote AI processing.

Evidence (sanitized preview)

POST /api/login HTTP/1.1
Status: 200 OK
{"ok":true,"accessToken":"[REDACTED]","user":{"id":"u_123"}}

// client
.catch((err) => console.error(err)); // no setUser / no navigate

Limitations

  • This example report is illustrative. Hypotheses are not confirmed without your project source.
  • Automated analysis cannot see private backend logs unless you paste them.