简体   繁体   中英

Passport - Can't set headers after they are sent

I'm making a login module, but whenever I put wrong email and password(is hashed, so i need to transform plaintext to hashed), it still goes through and on my node module, it shows this error:

Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/mnt/d/WEB/GITHUBREP/node_final/node_modules/express/lib/response.js:771:10)
at ServerResponse.location (/mnt/d/WEB/GITHUBREP/node_final/node_modules/express/lib/response.js:888:15)
at ServerResponse.redirect (/mnt/d/WEB/GITHUBREP/node_final/node_modules/express/lib/response.js:926:18)
at allFailed (/mnt/d/WEB/GITHUBREP/node_final/node_modules/passport/lib/middleware/authenticate.js:145:20)
at attempt (/mnt/d/WEB/GITHUBREP/node_final/node_modules/passport/lib/middleware/authenticate.js:180:28)
at Strategy.strategy.fail (/mnt/d/WEB/GITHUBREP/node_final/node_modules/passport/lib/middleware/authenticate.js:297:9)
at verified (/mnt/d/WEB/GITHUBREP/node_final/node_modules/passport-local/lib/strategy.js:82:30)
at User.findOne.then.user (/mnt/d/WEB/GITHUBREP/node_final/config/passport.js:13:18)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)

and my code for my passport is:

const bcrypt = require('bcrypt')
const localStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');

const User = require('../models/usrschema')

module.exports = function(passport){
  passport.use(
    new localStrategy({ usernameField: 'email'}, ( email, password, done) =>  {
      User.findOne({email: email})
      .then(user => {
        if(!user){
          return done(null, false, {});
        }

        bcrypt.compare(password, user.password, (err, isMatch) => {
          if(err) throw err;
          if(isMatch){
            return done(null, user);
          } else {
            return done(null, false);
          }
        });

      })
      .catch(err => console.log(err))
    })
  );
  //fsdfdsfds
  passport.serializeUser ((user, done) => {
    done(null, user.id)
  });

  passport.deserializeUser ((id, done) => {
    User.findById(id, (err, user) => {
      done(err, user)
    })
  })

}

Sorry i was busy.. in my loginrouter file,

pgroutr.post('/', (req, res, next) => {
  const emailog = req.body.email;
  var obj = {emaill: emailog}
  passport.authenticate('local', {
    // successRedirect: res.render('profile', obj), <- replace this with
       successRedirect: '/profile', //<----- this
    failureRedirect: '/'
  })(req, res, next);

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM