[英]Sails.js Node server req.session is always empty, using Passport-local strategy.
[英]Sails: sails-mysql causing errors with passport-local?
我花了3天的大部分時間
我正在開發Sails應用程序,同時自學有關Sails和Node的知識。 我正在遵循本教程 ,盡管由於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
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
中的錯誤,缺少此注釋中提到的login()
函數的return
。 req.logIn()
部分應為:
req.logIn(user, function(err) {
if (err) return res.send(err);
return res.send({
message: info.message,
user: user
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.