Currently I am trying to save a JWT token in the clients cookies after a login fetch. When looking at the set-cookie response in the Network tab of dev tools I get This set cookie was blocked because its domain attribute was invalid with regards to the current host url.
This is the code I use to set the cookie:
const token = user.generateVerificationToken();
const cookieOptions = {
domain: 'http://dev.com:3000',
// expires: new Date(Date.now() + 60000),
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
httpOnly: true,
secure: req.secure || req.headers['x-forwarded-proto'] === 'https',
};
res.cookie('jwt', token, cookieOptions);
These are the cors settings:
app.use( cors({credentials: true, origin: 'http://dev.com:3000'}) );
This is what the fetch request looks like on the client:
let res = await fetch( 'http://api.dev.com:4000/login', {
body: JSON.stringify(data), // login data
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
credentials: 'include'
})
/etc/hosts
:
127.0.0.1 dev.com
127.0.0.1 api.dev.com
Can anyone help with this? I can't find what needs to be done to fix this.