#Custom Auth + Refresh Token

14 messages · Page 1 of 1 (latest)

obtuse valleyBOT
#

Thanks for posting in #1088161997662724167.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.

    - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
    - Use [search.convex.dev](https://search.convex.dev) to search Docs, Stack, and Discord all at once.
    - Additionally, you can post your questions in the Convex Community's #1228095053885476985 channel to receive a response from AI.
    - Avoid tagging staff unless specifically instructed.

    Thank you!
hard saffron
#

some screenshots

#

sorry I want to edit post, but accidentially deleted the content of description
writting again now

void dagger
#

Suggestion (1) -- there's a verbose option for ConvexReactClient that might print some more info. Suggestion (2) -- does wrapping your getToken function in a useCallback change anything?

hard saffron
#

Convex:

Environment:

  • React (SPA)

Issue:

  • getToken seems force to refresh token in loop

Inspect Network WS
See Authenticate event

but right after seconds, it shows Transition and Authenticate event, force to call refresh token

on logs dashboard, it set from having data to null

void dagger
#

This looks like it could either be the Convex library thinking it needs to fetch new tokens all the time, or a React re-rendering too much issue (which I personally solve by wrapping anything I can think of in a useMemo / useCallback , seeing if that helps, and then cleaning it up later)

hard saffron
#

@void dagger Thanks for pointing out, let me debug your points

#

@void dagger you are right
after I memorized the getToken, it seems work

however, seems like it still refreshes token once at start?

#

however I think it's good to close this question.

Thanks @void dagger

#

Marked as resolver

Solution: Memorized getToken to avoid re-rendering from React

void dagger
#

however, seems like it still refreshes token once at start?
Yeah this is expected behavior. The Convex client tries to proactively fetch a new token a few seconds before it expires, but to guard against client / server clock skew, it does a trick where it issues a new token on initial load, and determines when to refetch by subtracting the issued time and the expiration time (https://github.com/get-convex/convex-js/blob/2b1f17136e66dfb788038bb9a84c05c654f09639/src/browser/sync/authentication_manager.ts#L330 if you're curious)

GitHub

TypeScript/JavaScript client library for Convex. Contribute to get-convex/convex-js development by creating an account on GitHub.

junior badge
#

@void dagger

#

When refreshing the token it unmounts the children of the <Authenticated> component?