problem next.js server component with cookie and nest.js backend
I setup better-auth with nestjs
and deploy in production with domain e.g api.example.com.
and try use nextjs. in development on localhost:3000
when call
apiClient.signIn.email everything ok and cookie is set but in server component and call
the data session is null and i checked cookie in header i don't see anything related better-auth why?
do i miss something?8 Replies
I'm having the same issue
🙁
@h_mz17 or @Ata Sanchez Did you find a solution for this, I'm having the same iissue. And for me everything works well locally and when Run in production with custom doamain it does not.
Hey @nelsonmandeladev just created specific endpoints on my API, and hit this to get the data for session/organizatio/etc.
the client doesn't work
Yes in cookie option must samesite none and also i add rewrite url in next.js and everythings work and then add coockie to fetch headers request
Hello @Ata Sanchez @nelsonmandeladev @h_mz17,
In server-side (RSC, Route Handlers), browser cookies are not automatically forwarded. I’ve personally followed this pattern when using Better Auth with Next.js:
-
auth.ts: main auth instance
- auth-client.ts: client instance (for client-side)
- auth-server.ts: client instance (for server-side)
I use auth.ts for the core instance, and when triggering requests from the server side, I use auth-server.ts. I’ve used this approach even before joining Better Auth, and it’s been my personal convention when working with Next.js. The auth-server.ts looks like this:
This is just my personal convention, but I’m sharing it because I had this experience when I first started using Better Auth.
I’ll try it tomorrow! Thanks @Taesu