我正在创建一个登录模块,但是每当我输入错误的电子邮件和密码(被散列,因此我需要将纯文本转换为散列)时,它仍然会通过并在我的节点模块上显示以下错误:
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)
我的护照代码是:
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)
})
})
}
答案 0 :(得分:0)
对不起,我在我的loginrouter文件中忙..
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);
})