Question

Express session not sending cookie to client side when nodejs app is deployed on nginx live server

I have nodejs application cookies working fine on localhost but when i deploy it on nginx live server cookie not set on client side. Please anyone to assist me on this matter. Below is the code am using to set up express session with cookie.

//express-session configurations
const storeOptions = {
  host: process.env.DB_HOST,
  port: process.env.DB_PORT,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  clearExpired: true,
}; //db store

const sessionStore = new MySQLStore(storeOptions);
const sessionOptions = {
  key: process.env.SESSION_KEY,
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  store: sessionStore,
  cookie: {
    maxAge: 1000 * 60 * 60 * 12, //12hours
  },
};

if (app.get("env") === "production") {
  app.set("trust proxy", 1); // trust first proxy
  sessionOptions.cookie.secure = true; // serve secure cookies
}

app.use(session(sessionOptions));

app.use(passport.initialize()); //initialize passport
app.use(passport.session()); //initialize session with passport

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Did it resolve?

Hi @samunyi90,

Have you checked the logs?

The server-side generate a HMAC signature of the session ID which is used by session-express, if it hasn’t been created correctly the client-side doesn’t will work properly.

You can uses the debug-module to output debug messages:

DEBUG=express-mysql-session* node your-app.js

I would be happy assist you,

Sergio Turpín

This comment has been deleted