passportjs local-strategy无法获得路线

时间:2016-07-25 17:19:11

标签: sqlite express passport.js sequelize.js

现在已经和这个问题搏斗了2天:)会感激一些帮助。

我正在尝试使用passportjs + sqlite + sequelize构建快速身份验证,

1)当我把路线放在app.use(passport.session())以上时,登录不起作用

2)当我把路线放在passport.session以下时,应用程序无法获取路线

App.js 



   --- normal includes
app.use(cookieParser());
app.use(session({
  secret: 'ilovescotchscotchyscotchscotch',
  resave: true,
  saveUninitialized: true
}));

var passport = require('passport');

app.use(passport.initialize());

app.use(passport.session());

var router = require('./routes/index')(passport);

app.use('/', router);

var initPassport  = require('./passp.js');
initPassport(passport);

护照档案

// load all the things we need
var LocalStrategy   = require('passport-local').Strategy;

// expose this function to our app using module.exports
module.exports = function(passport) {
    var User = require('./dbCon.js');
    passport.serializeUser(function(user, done) {
        done(null, user.dataValues.id);
    });

// used to deserialize the user
    passport.deserializeUser(function(id, done) {
        User.findOne({where : {id : id}}).then(function(user) {
            done(null, user);
        }).catch(function(err){
            console.log(err)
        });
    });


    passport.use('local-signup', new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'email',
            passwordField : 'password',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        function(req, email, password, done) {
            User.findOne({where : {username : email } })
                .then(function(user){
                    if(user === null) {
                        User.create({
                            username : email,
                            password : password
                        }).then(function(user){
                            done(null, user);
                        }).catch(function(err){
                            console.log(err)
                        })
                    }
                    else{
                        done(null, false);
                    }

                }).catch(function(err){
                console.log(err)
            })

        }
    ))

    passport.use('local-login', new LocalStrategy({
            // by default, local strategy uses username and password, we will override with email
            usernameField : 'email',
            passwordField : 'password',
            passReqToCallback : true // allows us to pass back the entire request to the callback
        },
        function(req, email, password, done) { // callback with email and password from our form
            return  User.findOne({where : {username : email } })
                .then(function (user) {
                    if(user === null) done(null, true)
                    if(user.dataValues.password === password)  done(null, user)
                    else{
                        done(null, true)
                    }

                })
        })
    )

};

路线档案

module.exports = function(passport){

router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

router.get('/error', function(req, res, next) {
    res.render('error', { title: 'Express' });
});

router.get('/admin', function(req, res, next) {
    res.render('admin', { title: 'Express' });
});


router.post('/signup', function(req, res, next) {
    passport.authenticate('local-signup', function(err, user, info) {
        if (err) { return next(err); }
        // Redirect if it fails
        if (!user) { return res.redirect('/'); }
        req.logIn(user, function(err) {
            if (err) { return next(err); }
            // Redirect if it succeeds
            return res.render('admin');
        });
    })(req, res, next);
});

router.post('/login', function(req, res, next) {
    passport.authenticate('local-login', function(err, user, info) {
        if (err) { return next(err); }
        // Redirect if it fails
        if (!user) { return res.redirect('/'); }
        req.logIn(user, function(err) {
            if (err) { return next(err); }
            // Redirect if it succeeds
            return res.render('admin');
        });
    })(req, res, next);
});

return router

} 请帮我!谢谢!

1 个答案:

答案 0 :(得分:1)

.then()收到结果作为第一个参数,而不是error

试一试:

// used to deserialize the user
passport.deserializeUser(function(id, done) {
    User.findOne({where : {id : id}})
    .then(function(user) {
        done(null, user);
    })
    .catch(function(err){
        done(err, null);
    });
});