Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Issue reading req.session.customProperty (express-session) from within express middleware

I have an issue with not being able to access a custom variable I set to the session object when a user logs into my service.

Post request from front-end

const sendChat = async (contents, cid, agentID) => {
  const config = {
    headers: {
      "Content-Type": "application/json",
    },
    withCredentials: true,
  };

  const body = JSON.stringify({
    channelID: cid,
    message: contents,
    agentID: agentID,
  });

  console.log(`AgentID: ${agentID}`);

  return await axios.post(
    "http://localhost:5000/api/chat/sendMessage/",
    body,
    config
  );
};

Session configuration in server

app.use(
  session({
    secret: config.get("sessionSecret"),
    name: "sid",
    cookie: { maxAge: 990000, httpOnly: false },
    resave: true,
    store: MongoStore.create({ mongoUrl: config.get("mongoURI") }),
    saveUninitialized: false,
  })
);

In both cases when I login to the service, through POSTMAN or with axios in the front-end, it creates a session containing the userID field that I can see when I check my store (mongoDB). However, when I get a request from axios, req.session.userID is undefined when try to access it in a route -- whereas with postman, it is defined when I try to access it in the route. I have cors enabled in the server.

const corsOptions = {
  origin: "http://localhost:3000",
  credentials: true,
  optionSuccessStatus: 200,
};

app.use(cors(corsOptions));

Why does it work with POSTMAN but not through the front-end localhost?

EDIT: I made a logical error in my front-end, I had added the withCredentials: true property to the wrong request.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!