Token refresh
Hello, I’m building my first project with Convex. I’ve used Supabase a lot, but after trying Convex, it has become my preferred BaaS!
A quick question, if I may: I’ve implemented authentication, but I can't find anywhere how token refresh is managed or if there’s a utility function for token refresh. Could you tell me something about this? Thanks!
1 Reply
How have you implemented authentiation? Generally the Convex client is passed a function to call to refresh its JWT through the .setAuth(getToken) method. If you're using React, this usually happens in a provider specific to the authentication provider you're using.
The Convex client does a few things with this function:
- it calls this function to retrieve a JWT and reads it to see when it expires. Then it scheduled a refresh to occur ~30 seconds before this expiration.
- when the WebSocket connection reports that an expired token is attempted to be used, the client reruns this getToken callback that was passed to getAuth(cb) in order to refresh it
there's some more info about specific flows in the docs, like https://docs.convex.dev/auth/clerk#under-the-hood