护照 - 发送后无法设置 headers
Passport - Can't set headers after they are sent
我正在制作一个登录模块,但每当我输入错误的电子邮件和密码(经过哈希处理,因此我需要将明文转换为哈希)时,它仍然会通过并在我的节点模块上显示此错误:
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)
})
})
}
抱歉,我很忙.. 在我的登录路由器文件中,
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);
})
我正在制作一个登录模块,但每当我输入错误的电子邮件和密码(经过哈希处理,因此我需要将明文转换为哈希)时,它仍然会通过并在我的节点模块上显示此错误:
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)
})
})
}
抱歉,我很忙.. 在我的登录路由器文件中,
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);
})