How is mutation authentication supposed to work?
In this template https://www.convex.dev/templates/nextjs-app-router
in the posts.ts inside the convex folder there is this code
There is no check for what user is running this code and seemingly no safe guards against a bad actor trying to post as another user.
Am I missing something or is there more to add for a production ready app?
Templates
The backend application platform with everything you need to build your product.
14 Replies
hi! this is just a basic example. if you want an authenticated mutation, you use
ctx.auth.getUserIdentity()
to check the logged-in useryou can see an example here ( https://github.com/AntonioErdeljac/next14-miro-clone/blob/master/convex/board.ts#L18 ) from @CodeWithAntonio 's most recent video
GitHub
next14-miro-clone/convex/board.ts at master · AntonioErdeljac/next1...
Contribute to AntonioErdeljac/next14-miro-clone development by creating an account on GitHub.
his is using clerk for auth
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
Thanks for the guidance. Yeah that makes sense, and i realized that a while ago. That "getUserIdentitity" function is used in the store function under the user.ts file on the template. Is there a way to enfore a unique on a schema value?
not "automatically", but mutations are ACID transactions (no data races). so if you check if it's there before you insert it, it's guaranteed unique
That's unreal, having no race conditions is a game changer. So if it's ACID is it single threaded aswell?
nope, not single threaded
this was a recent deep dive on @Web Dev Cody's server with a lot of detail about how it works: https://discord.com/channels/663478877355507769/1201995998503911424
Dang that link is #unknown
He's actually the only reason i'm checking out Convex lol
cool. well, in his yt-feedback channel, there's a whole thread that spawned out of this question on Jan 30

Thanks @Christian, I have fixed the template now:
https://github.com/get-convex/convex-nextjs-app-router-demo/blob/main/convex/posts.ts#L66-L80
GitHub
convex-nextjs-app-router-demo/convex/posts.ts at main · get-convex/...
Demo showing a Next.js App Router app powered by Convex backend - get-convex/convex-nextjs-app-router-demo
@Michal Srb Seems legit, thanks for the update. I am curious why you chose to use the tokenIdentifier instead of the subject.
From what i understand the difference between them is that
tokenIdentifier = issuer + subject(The issuer is a url, and the subject is the userId provided by clerk)
This is what the identity looks like when logged for clarification
This confused me too, as I saw a lot of guides using tokenIdentifier, and took a while to realize that subject is probably what I wanted.
tokenIdentifier allows you, in the future, use two different issuers (such as Clerk & Auth0) and not worry about subjects colliding. It's more defensive, but for most applications just using subject is fine