ForkMeDaddy
ForkMeDaddy13mo ago

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
ian
ian13mo ago
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
ForkMeDaddy
ForkMeDaddyOP13mo ago
Yes this doesn't really cover it from what I read
ForkMeDaddy
ForkMeDaddyOP13mo ago
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...
ForkMeDaddy
ForkMeDaddyOP13mo ago
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
Michal Srb
Michal Srb13mo ago
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
ForkMeDaddy
ForkMeDaddyOP13mo ago
oh bummer yes I will try some stuff but so far no luck 😦
Michal Srb
Michal Srb13mo ago
(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)
ForkMeDaddy
ForkMeDaddyOP13mo ago
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?
CodingWithJamal
CodingWithJamal13mo ago
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
ForkMeDaddy
ForkMeDaddyOP13mo ago
yes would love it ❤️
ForkMeDaddy
ForkMeDaddyOP13mo ago
@Michal Srb In the network tab I see this error:
No description
CodingWithJamal
CodingWithJamal13mo ago
sure, okay here is the Adapter, my convex schema.ts file was modeled from next-auths docs schema.ts the only thing i added was a user role function. hope this helps
Michal Srb
Michal Srb13mo ago
@ForkMeDaddy the token has to be a valid JWTToken
ForkMeDaddy
ForkMeDaddyOP13mo ago
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
Michal Srb
Michal Srb10mo ago
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.

Did you find this page helpful?