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

99
Visualizações
Check if express cookie-session has expired using passport isAuthenticated

I'm using express cookie-session: http://expressjs.com/en/resources/middleware/cookie-session.html

I can not find the answer to this and based on some logging there is no way to see the time elapsed. So I'm trying to find a way to check this and see if it has expired and log the user out and destroy the session.

In the isAuthenticated I'd like to see if there is a way to see if the session is expired. My hope was passport method isAuthenticated() would handle all that but it looks like this just checks if there is a user on req.user.

How would I find when the cookie has expired so I can log the user out ?

app.use(
  cookieSession({
    name: "authSession",
    expires: new Date(Date.now() + 1 * 60 * 1000),
    keys: [process.env.COOKIE_KEY],
  })
);

app.use(passport.initialize());
app.use(
  passport.session({
    saveUninitialized: true,
    resave: false,
    secret: process.env.SESSION_SECRET,
  })
);

app.get("*", isAuthenticated, (req, res) => {
  res.sendFile(indexPath);
});

I have a middleware checking if authenticated.

module.exports.isAuthenticated = (req, res, next) => {
  const expired = req.sessionOptions.expires;
  const current = new Date(Date.now());
  console.log("expired = ", expired, "  current = ", current);
  if (req.isAuthenticated()) {
    console.log(
    "req sessionn = ",
    req.sessionOptions.maxAge,
   );
    next();
  } else {
    res.render(process.env.AUTH_LOGIN_PAGE || "login");
  }
};
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