Cookies not working localhost when API already in aws lambda (hono)

I'm trying to work with front-end in localhost but using the api in aws lamba. The cookies not working. When sign-in, the request returns 200 but getCookies return null and not redirecting to dashboard. Anyone already had this issue?!
16 Replies
valentín
valentín4mo ago
Hi Guilherme, could you show your better-auth config?
Guilherme Marques
Guilherme MarquesOP4mo ago
No description
Guilherme Marques
Guilherme MarquesOP4mo ago
This a betterAuth config on my hono
valentín
valentín4mo ago
at the beginning I also had problems with the sessions, in my better-auth config after configuring these properties I had no more problems: storeSessionInDatabase: true, preserveSessionInDatabase: true
Guilherme Marques
Guilherme MarquesOP4mo ago
But i think, the problem is that my client not "see" cookies because is from my api domain (and my client is in localhost)
valentín
valentín4mo ago
mmm okey I understand, it could be problems with the trustedOrigins property?
Guilherme Marques
Guilherme MarquesOP4mo ago
i set both, but when sign-in the cookies not appears on devtools
sebastian
sebastian4mo ago
if you are in dev mode you should not have cross domain cookies enabled as localhost is the same domain you can use node_env variable to check if app is in prod and then set the boolean
Guilherme Marques
Guilherme MarquesOP4mo ago
my client is localhost, but my server is in aws lambda (not localhost)
sebastian
sebastian4mo ago
Then you should probably configure defaultCookieAttributes and trustedOrigins in your auth config. you can find everything in their docs
Guilherme Marques
Guilherme MarquesOP4mo ago
I tried to configured many thinks in both, but with no success, that's why i open this question
sebastian
sebastian4mo ago
Hmm. I've had a lot of problem with cookies honestly, but most of them were fixed by going into incognito. (Some thing were cached that werent supposed to) Which made debugging very hard. Maybe this would be your solution.
valentín
valentín4mo ago
guilherme, I share with you my better-auth configuration in the server: is a little long the configuration but maybe it will help you, as sebastian said and I told you before it must be a problem of the trustedOrigins or defaultCookieAttributes property.
Guilherme Marques
Guilherme MarquesOP4mo ago
Thanks! I'll take a look on this file!!
Firro
Firro4mo ago
If youre still trying for a solution, this is the only thing you need to do:
advanced: {
useSecureCookies: false,
cookies: {
session_token: {
// TODO: Make it lax for production
attributes: {
sameSite: "none",
},
},
},
advanced: {
useSecureCookies: false,
cookies: {
session_token: {
// TODO: Make it lax for production
attributes: {
sameSite: "none",
},
},
},
We have a staging setup but for our actual prod setup I would obviously restrict it to lax
valentín
valentín4mo ago
even with lax in development mode it should work the same, but just in case try with none

Did you find this page helpful?