Search code examples
expressherokucookiesdnscors

How can I ensure cookies are set after adding a domain on Heroku with CORS and DNS set up?


After adding a domain to heroku the cookies stopped being set. They are completely missing. They worked fine without the additional domain.

Thats my code. I tried adding cors but nothing changed.

const sessionOptions = {
    store,
    name: 'c_ie',
    secret,
    resave: false,
    saveUninitialized: false,
    proxy: true,
    cookie: {
        httpOnly: true,
        sameSite: 'none',
        secure: true, //https
        expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
        maxAge: 1000 * 60 * 60 * 24 * 7
    }
}

app.use(session(sessionOptions));

app.use(
    cors({
        origin: "https://mydomain.herokuapp.com/",
        credentials: true
    })
);

app.set('trust proxy', 1);

I added "two" domains to Heroku: www.mydomain.com and mydomain.com

DNS runs threw cloudflare.

www domain: CNAME www link by heroku that points to the www domain non-www domain: CNAME @ link by heroku that points to non-www domain

the non www-domain is being redirected to the www domain.

I tried changing the sessionOptions a lot but nothing seems to work for me. Have been trying the past 2 weeks..


Solution

  • Solution:

    I switched cloudflare proxy to dns only. Thats it.