ianpaschal
ianpaschal2w ago

getAuthUserId behaves oddly

In many of my functions, I use const userId = await getAuthUserId(ctx); to get the current user ID and check that it matches user IDs within requests. Works great! To my surprise though, while testing, it stopped working. So I added const userIdentity = await ctx.auth.getUserIdentity() and found that the value returned by getAuthUserId(ctx) was actually equal to subject rather than _id. How come? Is getAuthUserId first trying to get the ID and if it doesn't exist it returns the session? It can't always be using subject otherwise none of my functions would be working.
3 Replies
Convex Bot
Convex Bot2w ago
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 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!
sshader
sshader2w ago
Just to link to some relevant code getAuthUserId just calls auth.getUserIdentity, and then splits subject on | to return the user ID (code) Tokens get issued with sub as <userId>|<sessionId> (code) I'm not sure what you mean by "the value returned by getAuthUserId(ctx) was actually equal to subject rather than _id". Could you say more?
GitHub
convex-auth/src/server/implementation/index.ts at ffba54c0b3bd9d98e...
Library for built-in auth. Contribute to get-convex/convex-auth development by creating an account on GitHub.
GitHub
convex-auth/src/server/implementation/tokens.ts at ffba54c0b3bd9d98...
Library for built-in auth. Contribute to get-convex/convex-auth development by creating an account on GitHub.
ianpaschal
ianpaschalOP2w ago
I think I've solved my question. I sort of instinctively started filling in withIdentity({ _id: 'foo' }) but what I should have done is withIdentity({ subject: 'foo' })

Did you find this page helpful?