Using multiValueHeaders to return multiple cookies works in local development but not in production

One issue is process.env.ENV is meant to read process.env.NODE_ENV.

And as I was reminded (I just never learn sometimes!) after a little searching

So you need to set NODE_ENV in the Netlify UI to production.

This combined with the const sessionCooke = ... you have and the multiValueHeaders will work.

Example:

const cookie = require('cookie')
exports.handler = async (event, context, callback) => {

  var token = "KJASDIU34567ASKDGA"
  const sessionCookie = cookie.serialize('_auth', token, {
    secure: process.env.NODE_ENV === 'production',
    domain: process.env.NODE_ENV  === 'production' ? '.mydomain.com' : 'localhost',
    httpOnly: true,
    path: '/',
    maxAge: 3600
  });

  return {
    statusCode: 200,
    headers: {
      "Cache-Control": "no-cache"
    },
    multiValueHeaders : {
      "Set-Cookie": [
        'language=Dansk',
        sessionCookie
      ]
    },
    body: JSON.stringify(event.body)
  };
};

Learned more about cookies today than any previous day of my life. Still prefer chocolate chip though :smiley:

1 Like