amir
amir2d ago

Convex Auth JWT Subject

Hey guys, I noticed that every time I signOut and then signIn again, the sub field in my JWT token changes The first part is remaining constant which is the userId but then the second part is changing. For example: Initialy when I logged in the sub field in decoded JWT token was k172bx4p67v9qdhtac9n508ckn76vs10|jn73yr27ask6fsjjs8mzr4xhm576tczh After I signed out and signed in again it became k172bx4p67v9qdhtac9n508ckn76vs10|jn77eveqqnxdw9c78shea6r26s76vynz where k172bx4p67v9qdhtac9n508ckn76vs10 is the user id I need a constant, non-changing sub field. Is it possible to do that?
27 Replies
Convex Bot
Convex Bot2d 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!
ballingt
ballingt2d ago
@amir Can you say more about your setup? Where is the JWT coming from?
amir
amirOP2d ago
What specifically? Im using NextJs with Convex Auth
ballingt
ballingt2d ago
Ah the Convex Auth bit mostly
amir
amirOP2d ago
Right
ballingt
ballingt2d ago
sorry I see it in the subject now What do you need a constant sub field for? Are you using the JWT for other things, or is this because you're using it directly via await ctx.auth.getUserIdentity()?
amir
amirOP2d ago
Im passing it to third party system to generate web3 wallet for users, and because of the sub field changing, its generating new wallets each time
ballingt
ballingt2d ago
let's look at how this is set So would some other field work just as well for you? Are you using a Users table?
amir
amirOP2d ago
const token = useAuthToken() im getting the token using this hook, and passing it to the third party system, its looking for the sub field as far as I can tell to generate wallets
ballingt
ballingt2d ago
The docs for useAuthToken() say
* You should not pass this token to other servers (think of it as an "ID token").
Can you generate a different ID and store it in your users table?
amir
amirOP2d ago
With the same JWT_PRIVATE_KEY thats used by the Convex Auth backend?
ballingt
ballingt2d ago
No, just a random ID
amir
amirOP2d ago
Im not sure Im getting you, why?
ballingt
ballingt2d ago
It sounds like you want to use either the subject field or the JWT for something it's not designed for, I'm trying to find a way you could do something else DO you need a whole JWT, or just that field?
amir
amirOP2d ago
Right the whole JWT As its validating against the JWKS URI
ballingt
ballingt2d ago
Got it, so you want to use Convex Auth JWT for authenticated with more that just Convex, but as a general JWT for other services
amir
amirOP2d ago
Right
ballingt
ballingt2d ago
Hmm need to think about it, that's not something we built it for
amir
amirOP2d ago
How is the sub field being generated currently for JWT? Is there no easy way to tweak it, so that the sub is just constant userId Always
amir
amirOP2d ago
Just trying to understand why is the session id needed?
ballingt
ballingt2d ago
could you file an issue? this would be a new feature, we need to think through this
amir
amirOP2d ago
Usually sub is just the user id
ballingt
ballingt2d ago
yeah good question, it'll take some research to figure out
amir
amirOP2d ago
Okay, in the meanwhile is there a way for me to fix that on my end? To return userId as sub? Maybe patch the npm module or smth
ballingt
ballingt2d ago
you can always fork the library, but it's worth understanding how this is used. I'd probably create a new JWT for these users if you need this, then delegate between Convex auth's JWKS URI and your own JWKS URI or if you're in a hurry, consider Clerk — I bet they have this feature
amir
amirOP2d ago
Yeah, thats what I was thinking too as a last resort Gotcha, thanks @ballingt