Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda