我正在关注passport.js doc,以便使用Sequelize作为我的postgres数据库的ORM创建一个LocalStrategy。但是,在身份验证期间,在login.js模块中执行User.findOne(...).success(function(user){...]});
会返回undefined
。我做错了什么?
user.js的:
var pg = require('pg');
var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
port: 5432,
dialect: 'postgres'
});
var User = sequelize.define('users', {
username: Sequelize.STRING,
password: Sequelize.STRING
});
User.sync();
module.exports = User;
我的login.js(路由器)
var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
console.log(id)
User.findById(id, function(err, user){
done(err, user);
});
});
passport.use(new LocalStrategy(
function(username, password, done){
var User = require('../models/user');
User.find({where:{username: username, password: password}}).success(function(user){
if(!user) {
return done(null, false, {message: 'Nom d\'usager incorrect.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Mot de passe incorrect.' });
}
return done(null, user);
});
}
));
router.post('/login', passport.authenticate('local', { successRedirect: '/decoupage',
failureRedirect: '/login',
failureFlash: true })
);
module.exports = router;
答案 0 :(得分:17)
Sequelize使用bluebird
promise库的修改版本,该库没有.success()
函数。
相反,请使用.then()
(也遵循Promises/A+标准):
User.find(options).then(
function(user) { ... },
function(err) { ... }
);