-
Notifications
You must be signed in to change notification settings - Fork 57
Description
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:
- User changes their password.
- My auth library issues a new JWT/session id. I pass this as a dependency to
fetchAccessToken
, sofetchAccessToken
re-runs to grab this new access token, This causesConvexAuthStateLastEffect
to run client.clearAuth() - 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:

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:
- Convex should not re-run subscribed queries against the temporary unauthenticated state. Probably this means not sending the intermediate
tokenType: None
whenfetchAccessToken
is changed. I've confirmed that commenting outclient.clearAuth
inConvexAuthStateLastEffect
works. - 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.