signup with passport local authentication not working in node.js
<p>I am trying to create passport-local sign-up functionality, but whenever I try to hit auth/signup API, I always redirect to failure path i.e. <em>failureRedirect: '/auth/failure'</em>.</p>

<p>The code seems fine to me.</p>

<p><strong>app.js:</strong></p>

<pre><code>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);
</code></pre>

<p><strong>authenticate.js:</strong></p>

<pre><code>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;
}
</code></pre>

<p><strong>passport-init.js:</strong></p>

<pre><code>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);
    };
};
</code></pre>

<p><a href="https://i.stack.imgur.com/wMdqX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wMdqX.jpg" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/yG93y.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yG93y.jpg" alt="enter image description here"></a>
<a href="https://i.stack.imgur.com/vPfq6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vPfq6.jpg" alt="enter image description here"></a></p>