Couldn't set cookie, nestjs
Project ID: eb9aa9c1-aabe-4f51-9a40-40193647b820
i deploy frontend to vercel, backend to railway.
my route that set cookie 'res
.cookie('jwt-token', refreshToken, {
httpOnly: true,
secure: true,
sameSite: 'none',
maxAge: 30 * 24 * 60 * 60 * 1000,
})
.json({ accessToken, id: user.id })'. in main.ts i added cors 'app.use(cors({ origin: process.env.CLIENT_URL, credentials: true }));'. my cookie don't setting
18 Replies
Project ID:
eb9aa9c1-aabe-4f51-9a40-40193647b820
is your frontend using a different domain than the one on railway?
yes. frontend - https://talk-plus-client.vercel.app/, backend - https://talk-plus-server-production.up.railway.app/
you're trying to set a cookie for another domain which is not possible, in this case your frontend.
you'll need to specify the
domain
attribute in your res.cookie
for your backend domainyes, but instead of your frontend domain, specify your backend domain
i recommend you using railway's environment variable
process.env.RAILWAY_STATIC_URL
(this variable will be available in your backend application)i can use variable process.env.SERVER_URL
if you've that variable you can use the railway's environment variable instead, it's automatically available at runtime and points at your domain
Oops, you shouldn't place
https://
only the domain is necessary, sorry.
(if you're using RAILWAY_STATIC_URL
then it's not necessary to remove https://
)domain: talk-plus-server-production.up.railway.app
yes
Sorry, but i don't see cookie in Application -> Cookies
that only shows the cookies for you current domain, which is the frontend domain.
if you see, now all requests to the backend are with the
cookie
headeron laptop works, after i logged, i always push to /chat if i logged, and if i log out, i don't open /chat, it's right. but on phone after i logged, after reload i don't open /chat
it kinda depends on the browser.
Some browsers block cookies from other domains due to privacy settings
if you want more info https://stackoverflow.com/questions/72204984/cookies-not-storing-on-mobile-browsers-but-working-on-desktop
this can happens on desktop too, firefox offers that
there is another way how to solve that
the only way i see to resolve this is to buy a domain.
or you can just ditch cookies and set the
Bearer
header in every request