Next Auth
I am trying to implement next-auth with convex, so I have the user data inside the context. Has anybody a working example with it?
16 Replies
try searching discord for next-auth and report back if you don't find what you're looking for. someone just asked about this I think
Yes this doesn't really cover it from what I read
I am trying to do it like here: https://stack.convex.dev/custom-functions
Customizing serverless functions without middleware
Re-use code and centralize request handler definitions with discoverability and type safety and without the indirection of middleware or nesting of wr...
I try to read the session from next-auth there but when I use the getSession() fn i have an error saying it is "called by client"
but I can't use the server methods because I have no request obj. inside these functions
Convex acts as a third party API accessed from the client. So you need to figure out how to authenticate third party APIs from the client using NextAuth. I don't think NextAuth is very good at this right now. You could create a custom JWT token, and use this approach for Convex: https://docs.convex.dev/auth/custom-auth
Or you could store sessions in Convex (build a NextAuth adapter), pass session ID to the client from your Next server, pass that through your functions calls, then check the session in Convex.
We haven't done either yet, and I don't think anyone in the community has succeeded at putting together an example either.
Custom Auth Integration | Convex Developer Hub
Convex can be integrated with any identity provider supporting the
oh bummer
yes I will try some stuff but so far no luck 😦
(I should note that the reason NextAuth is not good at this is at people often proxy third-party APIs through the Next.js server, but this is not possible for WebSockets, which is what ConvexReactClient uses)
it shouldn't be hard would one think lol
if there is a session cookie = authenticated
otherwise not 😄
the convex code runs on the server right? but there you don't have access to the request because it's websocket and not a HTTP request?
hey
i have used next-auth and convex in a web app before if you wish to see it
its using quik and auth.js (next auth v2) but you should be able to port the Adapter
yes would love it ❤️
@Michal Srb In the network tab I see this error:

@ForkMeDaddy the token has to be a valid JWTToken
Thank you very much. This seems very promising.
Need to ask again. I can't use custom auth without an Identity provider?
I use next-auth in combination with walletconnect and SIWE for a web3 app. ideally I would continue using convex but I just dont see how I can do the authentication
I mean I could check it when I am calling functions etc.
guess it will lead to this
No problem!
update: im working on this now https://discord.com/channels/1019350475847499849/1019350478817079338/1210313559708278814
We now have a guide for setting up Convex with NextAuth: https://stack.convex.dev/nextauth
Convex with Auth.js (NextAuth)
Learn how to use Auth.js with your Next.js server and Convex backend to build a full-featured authentication system.