Rodrigo-R
Rodrigo-R•6mo ago

convexAuth id of the current user on the client?

Hi, i know how to do it on the backend functions (const userId = await auth.getUserId(ctx); ) But how to get currently loged user in the client side? I want to display email an image in the avatar.. Regards (sorry if it too obvious, but i jhave read the docs several times and i didn't found it)
7 Replies
ian
ian•6mo ago
One way to get the logged in user's details for displaying on the client would be to have a query that fetches the logged in user's ID, then does a ctx.db.get with that ID and returns the fields you want to expose to the client (or just the whole object). That way it automatically stays up to date as fields change (vs. the local auth token which is only updated on login) then on the client side you use useQuery to fetch the user's details
ian
ian•6mo ago
GitHub
llama-farm-chat/convex/users.ts at ef80c7e1926f6a91129056497a8b5382...
Use locally-hosted LLMs to power your cloud-hosted webapp - get-convex/llama-farm-chat
Rodrigo-R
Rodrigo-ROP•6mo ago
Thank you Ian, i ended up doing exactly that. I wa hoping there could be a more "fast" option, since i have to wait for the query to load (know i get why a AuthLoading is needed 😉 ) Also i don't really need reactivity for that query, but i guess it'll have to do it . All the best!
ian
ian•6mo ago
To just get the user ID you can decode the JWT locally and parse it out - but my guess is you want more than just the ID
Rodrigo-R
Rodrigo-ROP•6mo ago
indeed, but that is a good idea, i can manage with email, picture and name, i pressume that's also in the token thanks!
ian
ian•6mo ago
that's not currently in the token - and I'd suggest getting that from a query so it's always up to date (e.g. when they change their profile picture, it's annoying when it doesn't update everywhere immediately)
Michal Srb
Michal Srb•6mo ago
Query is the recommended way to go atm. Tracking a similar feature request in https://github.com/get-convex/convex-auth/issues/25
GitHub
Allow storing information in the JWT · Issue #25 · get-convex/conve...
Some people might be more familiar with retrieving information from the JWT instead of from the DB. The client would provide the equivalent of Clerk's useUser hook, and on the backend await ctx...

Did you find this page helpful?