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?
1 Reply
Ata Sanchez
Ata Sanchez•4d ago
I'm having the same issue šŸ™

Did you find this page helpful?