NodeJS护照未知身份验证策略

时间:2015-08-19 07:54:17

标签: javascript node.js express passport.js passport-local

我目前正在使用护照身份验证的节点js项目。 但我收到以下错误消息:

错误:“未知的身份验证策略”

这是我的代码:

LocalStrategy = require('passport-local').Strategy;
var UserModel = require('../models/user');

module.exports = function(passport){
passport.serializeUser(function(user, done){
    done(null, user.id);
});
passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user){
        return done(err, user);
    });
});

passport.use('local-signup', new LocalStrategy({
    passReqToCallback: true
}, function(req, username, password, done){
    process.nextTick(function(){
        User.findOne({username: username}, function(err, user) {
            if(err){
                return done(err);
            }

            if(user){
                return done(null, false, req.flash('signUpErr', 'The mail is taken'));
            } else {
                var newUser = new UserModel();
                newUser.name = username;
                newUser.password = newUser.generateHash(password);

                newUser.save(function(err){
                    if(err){
                        throw err;
                    }
                    console.log('All Ok');
                    return done(null, newUser);
                })
            }
        });
    });
}));

}

路线:

//Post requests
app.post('/signup', passport.authenticate('local-signup', {
    successRedirect: '/',
    failureRedirect : '/signup'
}));

我的模特:

var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

var UserSchema = new mongoose.Schema({
    name: String,
    password: String
});

UserSchema.method.generateHash = function(password){
    return bcrypt.hash(password);
};
module.exports = mongoose.model('UserModel', UserSchema);

我真的需要你的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

检查您是否将策略与用户凭据一起传递

 var credentials = {
     username:"username",
     password:"password",
     strategy:"local"

            };
 $http.post('auth/signin', credentials);

服务器端

function passportAuthenticate(req, res, next, strategy) {
    passport.authenticate(strategy, function(err, user, info) {
        if (err || !user) {
            res.status(400).send(info);
        } else  {
            next(req, res, user);
        }
    })(req, res, next);
}