Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda