Clerk + Convex Identifier Recommendation
Hello!
I have a question regarding an identifier for Clerk in the database.
Clerk has 2 different ways of identifying users and it doesn't seem to be consistent in a way.
When using the currentUser() hook, it returns tokenIdentifier and subject. The tokenIdentifier is a combination of the clerk url and the subject.
However, when getting data using Clerk webhook, it returns a unique identifier named "id" which is the same as the value of the subject identifier.
My question now is: Which of this is recommended to use as a unique identifier in my Convex database so I can easily map Clerk users to their data in the database.
I was thinking of storing both the tokenIdentifier and subject/id but it might not be a good practice.
Lastly, the reason why i am asking this is because when performing queries or mutations on the database, i will mostly need to locate a specific user in the DB and it must be done using an identifier.
Thanks in advance.
2 Replies
It's certainly up to you. These new docs suggest "subject"/clerkID for simplicity:
https://docs.convex.dev/auth/database-auth#set-up-webhooks
and call it externalId.
There are some things to consider:
1. It's unlikely that Clerk IDs will collide with other providers
2. Are you even going to use other providers? If not you can rely on Clerk IDs.
3. Clerk already links the same emails into a single account. If you use multiple providers, do you want to link accounts additionally? Are you sure it's safe? etc.
I expect that for most apps using clerk ID is totally fine, and if new requirements arise later you can perform a migration.
Storing Users in the Convex Database | Convex Developer Hub
You might want to store user information directly in your Convex database, for
Alright. Thanks! 😁