注册护照本地身份验证在node.js中不起作用

时间:2016-09-25 08:26:54

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

我正在尝试创建护照本地注册功能,但每当我尝试点击auth / signup API时,我总是会重定向到失败路径,即 failureRedirect:' / auth / failure'

代码对我来说似乎很好。

app.js:

var passport = require('passport');
var api = require('./routes/api');
var authenticate = require('./routes/authenticate')(passport);
require('./models/models');
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/test-chirp");
var app = express();

app.use('/auth', authenticate);

//// Initialize Passport
var initPassport = require('./passport-init');
initPassport(passport);

authenticate.js:

var express = require('express');
var router = express.Router();

module.exports = function(passport){

    //sends successful login state back to angular
    router.get('/success', function(req, res){
        res.send({state: 'success', user: req.user ? req.user : null});
    });

    //sends failure login state back to angular
    router.get('/failure', function(req, res){
        res.send({state: 'failure', user: null, message: "Invalid username or password"});
    });

    //sign up
    router.post('/signup', passport.authenticate('signup', {
        successRedirect: '/auth/success',
        failureRedirect: '/auth/failure'
    }));

    return router;
}

护照init.js:

var mongoose = require('mongoose');
var User = mongoose.model('User');
var LocalStrategy = require('passport-local').Strategy;
var bCrypt = require('bcrypt-nodejs');

module.exports = function (passport) {
    passport.serializeUser(function (user, done) {
        //return the unique id for the user
        return done(null, user._id);
    });

    passport.deserializeUser(function (username, done) {
        User.findById(id, function (err, user) {
            if (err) { return done(err, false); }
            if (!user) { return done('user not found', false); }
            return done(user, true);
        })
    });



    passport.use('signup', new LocalStrategy({
        passReqToCallback: true
    },
        function (req, username, password, done) {
            // find a user in mongo with provided username
            User.findOne({ 'username': username }, function (err, user) {
                if (err) {return done(err);}
                // already exists
                if (user) {return done(null, false);} 
                else {
                    // if there is no user, create the user
                    var newUser = new User();

                    // set the user's local credentials
                    newUser.username = username;
                    newUser.password = createHash(password);

                    // save the user
                    newUser.save(function (err) {
                        if (err) {throw err;}
                        console.log(newUser.username + ' Registration succesful');
                        return done(null, newUser);
                    });
                }
            });
        })
    );

    var isValidPassword = function (user, password) {
        return bCrypt.compareSync(password, user.password);
    };
    // Generates hash using bCrypt
    var createHash = function (password) {
        return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null);
    };
};

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

var app = express();

app.use('/auth', authenticate);

//// Initialize Passport
var initPassport = require('./passport-init');
initPassport(passport);

在初始化护照之前,您将路由到/auth路径。

尝试切换:

var app = express();

//// Initialize Passport
var initPassport = require('./passport-init');
initPassport(passport);

app.use('/auth', authenticate);