我正在开发一个Sails应用程序,同时自学Sails和Node。我在关注this tutorial,虽然我不得不在这里和那里做一些修改,因为bcrypt的windows需求绝对是疯狂的,我得到了它的大部分工作。我的问题似乎在教程config/passport.js
文件中。我已经结束了修改的内容有点,因为我正在通过调试。
准确的误差是
C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.res.setHeader (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\node_modules\connect\lib\patch.js:134:22)
at ServerResponse.res.set.res.header (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:595:10)
at ServerResponse.res.send (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:151:12)
at ServerResponse.res.json (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:237:15)
at ServerResponse.res.send (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:139:21)
at C:\Users\Jhecht\Desktop\sails\students\api\controllers\AuthController.js:22:30
at C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\http\request.js:51:48
at pass (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\authenticator.js:287:14)
at Authenticator.serializeUser (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\authenticator.js:289:5)
at IncomingMessage.req.login.req.logIn (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\http\request.js:50:29)
at C:\Users\Jhecht\Desktop\sails\students\api\controllers\AuthController.js:21:17
at Strategy.strategy.success (C:\Users\Jhecht\Desktop\sails\students\node_modules\passport\lib\middleware\authenticate.js:194:18)
at verified (C:\Users\Jhecht\Desktop\sails\students\node_modules\passport-local\lib\strategy.js:83:10)
at C:\Users\Jhecht\Desktop\sails\students\config\passport.js:28:5
at returnResults (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\query\finders\basic.js:180:9)
Program exited with code 1
config/passport.js
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bcrypt = require('bcrypt-nodejs');
passport.serializeUser(function (user, done) {
done(null, user.user_id);
});
passport.deserializeUser(function (id, done) {
Users.findOne({
user_id: id
}, function (err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy({
usernameField: 'user_email',
passwordField: 'user_password'
},
function (email, password, done) {
Users.findOne({
user_email: email
}, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Email not found, are you sure you registered?'
});
}
if (bcrypt.compareSync(password, user.user_password)) { //Changed from tutorial to see if the error was coming from the async function
console.info("User Found, Hashes are equal."); //Me finding errors
done(null, user, {
message: 'Login Successful'
});
//Removing this line I don't get the error in the console and the server doesn't reset, but without it the authentication doesn't work
}
});
}
));
不幸的是,我对Sails/Node的了解还不够,无法弄清楚到底发生了什么。这个错误似乎是由sails-mysql适配器抛出的,但是 * 只有 * 在调用done()
函数时才会抛出。但是,如果没有done()
调用,我就无法让Passport对用户进行授权。
1条答案
按热度按时间o4tp2gmn1#
本教程在
AuthController.js
中包含一个错误,在login()
函数中缺少一个return
,如本注解中所述。req.logIn()
部分应为: