Skip to content

Changing fetchAccessToken causes all Convex queries to throw unauthenticated errors #82

@widavies

Description

@widavies

I'm hitting a corner case in my application that causes Convex queries to fail when the JWT access token changes.

My setup is roughly:

  1. User changes their password.
  2. My auth library issues a new JWT/session id. I pass this as a dependency to fetchAccessToken, so fetchAccessToken re-runs to grab this new access token, This causes ConvexAuthStateLastEffect to run client.clearAuth()
  3. Convex immediately throws errors for all of my subscribed queries (they run against the clearAuth auth state)

You can see the failed queries in the sync network log:

Image

As I understand it, this error occurs because the auth state temporarily flickers to unauthenticated (the Authenticate, tokenType: None in the sync log). When the token is cleared, Convex re-runs subscribed queries against this unauthenticated state, all of which fail).

I double checked that my new JWT is valid and non-expired. It has valid iat, aud, sub, and exp claims.

I believe this may be a bug in Convex - Convex should not re-run queries on this intermediate identity state during a fetchAccessToken change.

I can think of two solutions:

  1. Convex should not re-run subscribed queries against the temporary unauthenticated state. Probably this means not sending the intermediate tokenType: None when fetchAccessToken is changed. I've confirmed that commenting out client.clearAuth in ConvexAuthStateLastEffect works.
  2. Provider the developer a way to indicate that the session is changing from one logged in session to another. Convex would then 1) unsubscribe all queries 2) push the new JWT/identity 3) resubscribe all queries.

In both cases, the developer would be responsible for unsubscribing all useQuery's that would fail on the new session (because of differing permissions) prior to changing fetchAccessToken.

This might also be related to expectAuth - maybe it should always apply, not just to the initial refetch.

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