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

246
Vistas
Error: Cannot set headers after they are sent back to the client in express middleware

I am having issues while doing some complex stuff inside middleware. I know i should be getting this done in a route or some small function but I had no choice.

This is the entire middleware:

app.use(async function addReqUser(req,res,next) {
    if (req.cookies) {
        let token = req.cookies['session-token'];
        let user = {};
    async function verify() {
    //google auth function
        const ticket = await client.verifyIdToken({
            idToken: token,
            audience: CLIENT_ID, 
        });

        const payload = ticket.getPayload();
        user.name = payload.name;
        user.email = payload.email;
        user.picture = payload.picture;
      }
      verify()
      .then(async ()=>{
          req.user = user;

          const user = await User.find({ email: req.user.email }).select("_id").exec();
          req.user.id = user[0]._id

          res.locals.user = req.user //populate the user with an id

          next();
      })
      .catch(err=>{
        console.log(err)
         return res.redirect('/')
      })
    }
    next()
})

The error just says [ERR HTTP_HEADERS_SENT] Cannot set headers after they are sent back to the client

Help me with this.

Your fast response in appreciated.

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

0

If req.cookies is set and the verify() call is successful, next() will be executed twice: once after the asynchronous invocation of verify() and once more at the end of the .then function.

On the other hand, if verify() fails, the res.redirect call in the .catch function sets a header after the first execution of next().

Both can lead to the observed error. A middleware should either

  • optionally set headers and then call next() (once!) or
  • optionally set headers and then send a response (e.g., via res.send), but not call next().
about 4 years ago · Juan Pablo Isaza Denunciar

0

You have not used async and await for verify function, you can modify you app.use to the following code

app.use(async function addReqUser(req, res, next) {
    if (req.cookies) {
        let token = req.cookies['session-token'];
        let user;
        async function verify() {
            //google auth function
            return client.verifyIdToken({
                idToken: token,
                audience: CLIENT_ID,
            });

        }
        const response = await verify()
        const payload = response.getPayload()

        if (payload.email) {
            user.name = payload.name;
            user.email = payload.email;
            user.picture = payload.picture;
         
            const userDoc = await User.find({ email: req.user.email }).select("_id").exec();
         
            req.user = userDoc;
            req.user.id = userDoc[0]._id;
            res.locals.user = req.user //populate the user with an id
            return next()

        } else {
            return res.redirect('/')
        }
    }
    next()
})

While using the .then on verify, the code is not waiting for verify() Promise to complete & next() after the if block is executed first

about 4 years ago · Juan Pablo Isaza Denunciar

0

The req.cookies was being called and the next() was being executed twice. After some suggestions and coding here's the solution:

app.use(function addReqUser(req, res, next) {
    
    if (Object.hasOwnProperty.bind(req.cookies)('session-token')) { //has a session-token
        let user = {};
        async function verify() {
            let token = req.cookies['session-token'];
            //google auth function
            const ticket = await client.verifyIdToken({
                idToken: token,
                audience: CLIENT_ID,
            });
            const payload = ticket.getPayload()

            if (payload) {
            user.name = payload.name;
            user.email = payload.email;
            user.picture = payload.picture;
            
            req.user = user;

            const userDoc = await User.find({ email: req.user.email }).select("_id").exec();
       
            req.user.id = userDoc[0]._id;
            res.locals.user = req.user //populate the user with an id

        } 

        }
        verify()
        .then(()=>{
          next();
      })
      .catch(err=>{
        console.log(err)
          res.redirect('/')
      })
    } else {
        next();
    }
})

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