How to get Clerk user current selected Organisation
Hello, I would like to be able to know which organisation the user belong to without having to pass it in the query.
Two problem:
- I added it to the clerk jwt, however in the getIndentity i do not have access to it.
- If I do this, how can I invalidate data for the client-side that changed organization ?
2 Replies
I would write the organization to the user document in Convex. Syncing it over via a Clerk webhook when it changes. You can also follow the pattern others have used where they set up Clerk to store the organization ID in the "gender" field of the JWT, so it can show up in the auth.getUserIdentity() object.
We have plans to support extra JWT claims so this workaround won't be necessary. but I'd search for threads with those workarounds for now
Once you have a client-side subscription that is reading the user, when the user's organization changes it will invalidate & re-run that query and send the new data to the client automatically, which is a benefit of storing it in Convex vs. just on the JWT
There is support for custom claims in Convex 1.14: https://news.convex.dev/announcing-convex-1-14-3/#support-for-custom-claims-in-jwts
Convex News
Announcing Convex 1.14
Convex Auth for Next.js server side auth. We made some changes to make ConvexReactClient to enable Convex Auth for Next.js server side authentication.