Code with AntonioC
Convex Communityβ€’3y agoβ€’
20 replies
Code with Antonio

Clerk Auth in Route Handlers

Hi everyone πŸ‘‹

I need help for a pretty specific edge case. A third party library which I use alongside Convex needs to generate an authentication token by using NextJS route handlers endpoint.

Lets call this
/api/third-party-auth


In this route handler, which is a POST request, I need to have access to currently logged in user.

I know I can access convex instance using the following:
const convex = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!);
But I am not sure if I can use that to access current auth.

This is usualy accessible with
@clerk/nextjs
package, but as far as I understand, for Convex + Clerk we use
@clerk/react
package instead, and then we access "backend" auth using
ctx.identity


How would I solve this specific issue? Here are a couple of things that come to mind:
- Add @clerk/nextjs & middleware.ts (I have a feeling this will break things)
- convex/http (I tried this, but did not really understand if I can test this out locally?)
- getToken I see this popping up in discord searches, but do not really understand how I should approach it.

My main goal is to get access to user ID inside route handler (I also need user organization ID, but I will debug that later)

Thanks!
Was this page helpful?