response.cookie() does not send cookie

Cing Sian Dal
1 min readMay 4, 2021

If you are trying to send cookies in http header in different ports or domain, you will need to allow withCredential: true in your CORS policy in your Express middleware.

app.use(cors({
origin: 'http://localhost:3000',
methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD'],
credentials: true
}));
// to enable CORS using credentials

`withCredentials` indicates whether or not cross-site Access-Control requests should be made using credentials.

For multiple arrays of whitelists,

const whitelist = [
'http://localhost:3000',
'http://localhost:4000'];
const corsOptionsDelegate = function (req: any, callback: any) {
let corsOptions;
if (whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = {
origin: true,
methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD'],
credentials: true
} // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, corsOptions);
// callback expects two parameters: error and options
}
app.use(corsOptionsDelegate);

If it does not work, leave your comment and your solution if you have found out.

--

--

Cing Sian Dal

Don’t follow me. I wrote junks here. Follow me on Twitter instead.