Sails: sails-mysql 导致 passport-local 出错?
Sails: sails-mysql causing errors with passport-local?
我在这上面花了 3 天的大部分时间
我正在开发一个 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 授权用户。
非常感谢您给我的任何指示。
本教程在 AuthController.js
中包含错误,在 this comment 中提到的 login()
函数中缺少 return
。 req.logIn()
部分应该是:
req.logIn(user, function(err) {
if (err) return res.send(err);
return res.send({
message: info.message,
user: user
});
});
我在这上面花了 3 天的大部分时间
我正在开发一个 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 授权用户。
非常感谢您给我的任何指示。
本教程在 AuthController.js
中包含错误,在 this comment 中提到的 login()
函数中缺少 return
。 req.logIn()
部分应该是:
req.logIn(user, function(err) {
if (err) return res.send(err);
return res.send({
message: info.message,
user: user
});
});