problem next.js server component with cookie and nest.js backend

I setup better-auth with nestjs
const auth = betterAuth({
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes: {
sameSite: 'none',
secure: true,
httpOnly: true,
partitioned: true,
},
useSecureCookies: true,
},
database: mongodbAdapter(db, {
client,
}),
trustedOrigins: ['https://api.example.com','http://localhost:3000'],
plugins: [
openAPI(),
bearer(),
username({
// eslint-disable-next-line @typescript-eslint/no-shadow
usernameValidator(username) {
if (username === 'admin') {
return false;
}
return true;
},
}),
phoneNumber(),
],
});
const auth = betterAuth({
emailAndPassword: {
enabled: true,
},
advanced: {
defaultCookieAttributes: {
sameSite: 'none',
secure: true,
httpOnly: true,
partitioned: true,
},
useSecureCookies: true,
},
database: mongodbAdapter(db, {
client,
}),
trustedOrigins: ['https://api.example.com','http://localhost:3000'],
plugins: [
openAPI(),
bearer(),
username({
// eslint-disable-next-line @typescript-eslint/no-shadow
usernameValidator(username) {
if (username === 'admin') {
return false;
}
return true;
},
}),
phoneNumber(),
],
});
and deploy in production with domain e.g api.example.com. and try use nextjs. in development on localhost:3000
export const authClient = createAuthClient({
baseURL: "https://api.example.com",
plugins: [usernameClient(), phoneNumberClient(), genericOAuthClient()],
});
export const authClient = createAuthClient({
baseURL: "https://api.example.com",
plugins: [usernameClient(), phoneNumberClient(), genericOAuthClient()],
});
when call apiClient.signIn.email everything ok and cookie is set but in server component and call
const data= await authClient.getSession({
fetchOptions: {
headers: await headers(),
},
});
const data= await authClient.getSession({
fetchOptions: {
headers: await headers(),
},
});
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
Ata Sanchez
Ata Sanchez2mo ago
I'm having the same issue 🙁
nelsonmandeladev
@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.
Ata Sanchez
Ata Sanchez4w ago
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
h_mz17
h_mz17OP4w ago
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
Taesu
Taesu4w ago
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:
Taesu
Taesu4w ago
No description
Taesu
Taesu4w ago
This is just my personal convention, but I’m sharing it because I had this experience when I first started using Better Auth.
Ata Sanchez
Ata Sanchez4w ago
I’ll try it tomorrow! Thanks @Taesu

Did you find this page helpful?