User not authenticated" error in Convex mutation when called from Next.js API route
Hello! I'm running into an authentication issue when calling Convex mutations from Next.js API routes. I'm getting "User not authenticated" error when calling ctx.auth.getUserIdentity() inside a Convex mutation that's being called from a Next.js API route. However, other mutations that also use ctx.auth.getUserIdentity() work perfectly when called from client components that are wrapped in Clerk's <Authenticated> component.
Next.js API route
Convex Mutation
Could I get some help on this?
4 Replies
fetchMutation (and fetchQuery/fetchAction/preloadQuery) accept a third args object of Nextjs options, you can pass a token there to authenticate the function call.Thank you so much @erquhart! It's working now 😊 yay
@erquhart is there some equivalent for executing mutations in a tanstack route's
beforeLoad? I'm having the exact same issue.To anyone stumbling upon this thread - my issue was that I used the session token, not the JWT template. See here: https://docs.convex.dev/client/react/tanstack-start/tanstack-start-with-clerk#:~:text=const%20token%20%3D%20await%20auth.getToken(%7B%20template%3A%20%27convex%27%20%7D). You have to pass
template: 'convex'. The strange thing is, requests made from components worked as expected, but the ones initiated from the server (route loader or beforeLoad) failed.TanStack Start with Clerk | Convex Developer Hub
Learn how to integrate Clerk authentication with Convex in TanStack Start applications using ID tokens and ConvexProviderWithClerk.