Setting auth on the server

I'm currently using NextJS, tRPC and Clerk to handle some logic outside of convex. Is it possible to set authentication values on the server? I'm using the convex.setAuth function w/ the Clerk token but I'm running into the following error whenever I try to call a convex function (with or without an auth middleware):
Error: {"code":"Unauthorized","message":"Authentication failed"}
Error: {"code":"Unauthorized","message":"Authentication failed"}
15 Replies
ballingt
ballingt2y ago
What do your server-side Convex calls look like, are you using the HTTP client?
Michal Srb
Michal Srb2y ago
@Chad Maycumber if you could share the code that calls convex.setAuth that might help us help you as well, thanks!
Chad Maycumber
Chad MaycumberOP2y ago
Yeah definitely! One second
Chad Maycumber
Chad MaycumberOP2y ago
No description
Chad Maycumber
Chad MaycumberOP2y ago
This then gets passed as a context value through tRPC so I can use it in the routes
ian
ian2y ago
If you're using our Clerk provider, you shouldn't need to be calling setAuth: https://docs.convex.dev/auth/clerk If you're going a custom route, then you can check out: https://docs.convex.dev/auth/custom-auth at a glance, the setAuth should take in a function, not a value: https://docs.convex.dev/api/classes/react.ConvexReactClient#setauth
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
Custom Auth Integration | Convex Developer Hub
Convex can be integrated with any identity provider supporting the
Chad Maycumber
Chad MaycumberOP2y ago
Thanks for the resources I'll give these a shot
Michal Srb
Michal Srb2y ago
From you screenshot it looks like you're not on the latest version of Convex? There have been many fixes to Auth recently. setAuth should take an async function in the latest version - but ideally as Ian suggested you want to use ConvexProviderWithClerk instead. Let us know how it goes
Chad Maycumber
Chad MaycumberOP2y ago
I'm on convex v13! I think the types for ConvexHTTPClient and the react client might be different? I can't use the Clerk provider here because I need the auth set in an HTTP request! Thanks for the help here
Michal Srb
Michal Srb2y ago
Ah, gotcha. In that case I would double/triple check that you have the correct combo of Publishable Key, Issuer URL and "convex" set up: The first in the Clerk provider and the latter two in convex.json (and deployed via convex dev or convex deploy).
Chad Maycumber
Chad MaycumberOP2y ago
I'm using it in combination with the Clerk provider and that one does seem to function fine passing the auth properly to the convex backend
Michal Srb
Michal Srb2y ago
Hey @Chad Maycumber did you verify that convex.json has the right issuer URL in domain and applicationID: "convex" in the authInfo field?
Chad Maycumber
Chad MaycumberOP2y ago
It does look like my issuers are correct, I do have two objects in the authInfo field (one for prod; one for dev).
Michal Srb
Michal Srb2y ago
Looking again at the screenshot above, the issue might be that getToken() should be getToken({ template: "convex")
Chad Maycumber
Chad MaycumberOP2y ago
I think this is working for me now thanks!

Did you find this page helpful?