I am working on a nodejs
backend and react
front-end application with jwt
authentication features. When authenticating users I need to send cookies to the client via Set-cookie header
. It works fine when I use localhost, but after deploying it on render
it does not work. The cookies are not getting saved. On the network tab, it gives a warning This attempt to set a cookie via Set-cookie header was blocked because its Domain attribute was invalid with regard to the current host url
.
This is my cookie configuration file
import urls from './urls.js';
const { hostname } = new URL(urls.CLIENT_URL);
const domain = `.${hostname.split('.').slice(-2).join('.')}`; //.onrender.com
const access = {
name: process.env.COOKIE_ACCESS_NAME,
options: {
sameSite: 'none',
domain,
httpOnly: false,
secure: true,
maxAge: process.env.ACCESS_EXP,
},
delete: {
sameSite: 'none',
domain,
httpOnly: false,
secure: true,
maxAge: new Date(null),
},
};
const refresh = {
name: process.env.COOKIE_REFRESH_NAME,
options: {
sameSite: 'none',
domain,
httpOnly: true,
secure: true,
maxAge: process.env.REFRESH_EXP,
},
delete: {
sameSite: 'none',
domain,
httpOnly: true,
secure: true,
maxAge: new Date(null),
},
};
export default { access, refresh };
Here, I have tried changing the domain as
.onrender.com
onrender.com
(Have typed manually to check on const domain = domain-name
)
Noting works. What is the issue here?