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

232
Vistas
req.session.returnTo is undefined

I am using using passport to authenticate my users using discord oauth2. I want them to be redirected back to the same page they came from instead of to the home page or a dashboard.

I tried storing the URL in the session like described here, but it doesn't persist to the next request.

Middleware for my pages that need authentication:

module.exports = (req, res, next) => {
  if (req.user) {
    next();
  }
  else {
    req.session.returnTo = req.originalUrl;
    res.redirect('/auth');
  }
};

Auth route:

router.get("/auth", passport.authenticate("discord"));

router.get("/auth/redirect", passport.authenticate("discord", {
  failureRedirect: "/auth/forbidden"
}), (req, res) => {
  console.log(req.session); // doesnt have returnTo inside anymore ?
  res.redirect(req.session.returnTo || '/');
  delete req.session.returnTo;
});

The console.log shows the user successfully authenticated, but the returnTo field is no longer there.

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