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

243
Vistas
Unable to redirect user to login page after authentication check in Express.js

I have been attempting to redirect users to a login page after submitting a request to the home page if they are not logged in, but they keep getting redirected to the home page regardless. I have a method that is used to verify if they are logged in and returns a boolean with their status, then I have written another helper function using this method to redirect the user:

const verifySession = (req, res, next) => {
  if (req.session) {
    models.Sessions.get({hash: req.session.hash})
      .then(sessionInfo => {
        return models.Sessions.isLoggedIn(sessionInfo);
      })
      .then (status => {
        if (!status) {
    console.log(‘status’, status);
          res.redirect('/login');
        } else {
          next();
        }
      });
  }
};

The console.log for status is returning false as expected, but still no redirect is occurring. I have tried using this helper function in several different ways in my express request handler:

app.get('/',
  (req, res) => {
    verifySession(req, res);
    res.render('index');
  });

Or:

app.get('/', verifySession,
  (req, res) => {
    res.render('index');
  });

Or also:

app.use('/', verifySession);

app.get('/',
  (req, res) => {
    res.render('index');
  });

All of these have failed to redirect the user, and they are still being sent to the home page even when the status is returning false and the res.redirect call should be occurring.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const verifySession = (req, res, next) => {
  if (req.session) {
   return models.Sessions.get({hash: req.session.hash})
      .then(sessionInfo => {
        return models.Sessions.isLoggedIn(sessionInfo);
      })
      .then (status => {
        if (!status) {
    console.log(‘status’, status);
          return res.redirect('/login');
        } else {
          return next();
        }
      });
  }
  return res.redirect('/login'); //if req.session is falsey
};
app.use('/', verifySession);

app.get('/',
  (req, res) => {
    res.render('index');
  });
about 4 years ago · Juan Pablo Isaza Denunciar
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