I am following the passport.js doc in order to create a LocalStrategy using Sequelize as ORM for my postgres database. However, during authentification, doing User.findOne(...).success(function(user){...]});
in my login.js module returns undefined
. What am I doing wrong?
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;
my login.js (router)
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;
Sequelize uses a modified version of the bluebird
promise library, which doesn't have a .success()
function.
Instead, use .then()
(which also follows the Promises/A+ standard):
User.find(options).then(
function(user) { ... },
function(err) { ... }
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With