I can't seem to set the cookies if I make the api call from Netlify, but with Postman it works.
I don't understand why.
My code looks like this:
router.post('/login', localAuth, async (req, res) => {
// The code goes through and returns status 200
return res.status(200)
.cookie('accessToken', accessToken, {
signed: true,
httpOnly: true,
secure: true,
maxAge: 15 * 60 * 1000,
sameSite: 'none', // <-- I also tried lax
}).cookie('refreshToken', refreshToken, {
signed: true,
httpOnly: true,
secure: true,
maxAge: 7 * 24 * 60 * 60 * 1000,
sameSite: 'none', // <-- I also tried lax
}).send( // something );
});
Then the code tries a different route right after which fails due to missing cookies
router.get('/user', accessjwtAuth <-- this fails due to no cookies, async (req, res) => {})
Netlify comes with SSL certificate by default. The call from the frontend looks like this:
const config = {
baseURL: `${API_URL}/api/auth/login`,
method: 'post',
withCredentials: true,
headers: {'Content-Type': 'application/json',},
data: values,
};
axios(config).then((res) => {});
Lastly, the express app is configured like this:
const allowed_origins = ["https://something.netlify.app", "localhost:8080"];
app.use(function(req, res, next) {
const origin = req.headers.origin;
if (allowed_origins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
};
res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
I keep getting this for my signedcookies, [Object: null prototype] {}
I noticed that this issue happens on Safari and not Chrome.
In Chrome, the req has both accessToken
& refreshToken
.
I also noticed that if I set sameSite: 'lax'
then only the refreshToken
gets preserved.