Sails:sails-mysql导致本地护照错误?

时间:2016-06-20 01:43:55

标签: javascript mysql sails.js passport-local passport.js

我已经花了3天的时间来完成这个

我正在研究Sails应用程序,同时自学Sails和Node。我关注This Tutorial,虽然我不得不在这里和那里做一些改变,因为bcrypt的窗户要求绝对疯狂,我得到了大部分工作。我的问题似乎出现在教程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

配置/ 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 个答案:

答案 0 :(得分:1)

本教程在AuthController.js中包含错误,错过了return函数login()中的req.logIn()req.logIn(user, function(err) { if (err) return res.send(err); return res.send({ message: info.message, user: user }); }); 部分应为:

class dabba {
int l, b, h;

dabba(int l, int b, int h) {
    this.l = l;
    this.b = b;
    this.h = h;
}

void dabbashow() {
        System.out.println("The variables are length:" + l + " breadth:" + b + " height:" + h + ".");
    }
}