Skip to content

Expired or logged-out sessions keep sending the old token from the Zustand auth store #188

Backlog

Description

@fernandatoledo

Problem

The two auth layers don't clear together. AuthProvider (auth.tsx) keeps its session in authStorage, and the Zustand store (lib/auth/index.tsx) keeps its own copy of the token in the general MMKV storage. Nothing that ends the session in AuthProvider touches the Zustand store:

  • Expiry: the AuthProvider request interceptor calls clearTokens() (auth.tsx#L65) and checkToken sets token to null, but the Zustand token stays set.
  • Logout: logout() (auth.tsx#L139-L141), used by Settings, only calls clearTokens(). signOut() from the Zustand store is never called anywhere in src/.
  • 401: once 401 responses never log the user out #185 is fixed, it goes through the same logout(), so it has the same gap.

The request interceptor in interceptors.ts#L19 attaches whatever useAuth.getState().token holds (Authorization, access-token, client, uid, expiry) without checking it. It is registered after the AuthProvider interceptor, and Axios runs request interceptors in reverse order, so it runs first, before AuthProvider gets a chance to clear anything. On the next launch, hydrateAuth() (_layout.tsx#L29) restores the same token from storage.

Impact

After the session expires or the user logs out, the UI shows them as logged out, but every API request still carries the old credentials, including across app restarts. A user who logs out on a shared device keeps sending their access-token/client/uid until another login overwrites them. Against a backend that still accepts the token, requests made "logged out" are made as the previous user.

Fix

Keep the dual-layer design and make authStorage the single source of truth for whether a session is valid:

  • Clear the Zustand store wherever AuthProvider ends the session (expiry, logout(), 401), e.g. by calling signOut() from one clearing path.
  • Have the Zustand request interceptor only attach the token when there is a valid session (getValidAccessToken() from feat(auth): protect deeplinked screens and return to them after login #183), and sign out otherwise.
  • Don't let hydrateAuth() restore a token when there is no valid session.

No token refresh. Add Jest tests that, after expiry and after logout, neither interceptor sends the old token and hydrateAuth() leaves the store empty.

Builds on #183 (getValidAccessToken(), syncSession()). Related: #185.

Found while reviewing #183.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions