MA Rahman MA Rahman - 3 months ago 38
Node.js Question

Register User Through Passport Js

i want to add new user from signup page through help of passport.js
Signup form is following

<form id="Signup-form" name="SignupForm" action="/signup" method="post"/>
<input type="text" id="firstname" name="Firstname" >
<input type="text" id="lastname" name="Lastname"/>
<input type="email" name="email" />
<input type="text" id="rollno" name="rollno"/>
<input type="password" name="password" id="password"/>
<input type="password" name="confirm" id="confirm-password"/>
<input type="radio" name='Gender' value="Male" />
<input type="radio" name='Gender' value="FeMale" />

my passport is initialized in app.js as


var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

after db setting


intialized as


post sign up route'/signup', passport.authenticate('local.signup' , {
successRedirect : '/home',
failuerRedirect : '/signup',
failuerFlash: true

my user model

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

const UserSchema = new Schema({
First_Name : String,
Last_Name : String,
email : String,
Roll_No : String,
Gender : String,
password : String
},{collection : 'Users'});

UserSchema.methods.encryptPassword = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null);

UserSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
var User = mongoose.model('User' , UserSchema);
module.exports = User;

now my passport.js file in config dir is

var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function (user, done) {

passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);

my main questions how to write strategy for this route with all fields

passport.use('local.signup', new LocalStrategy({
//strategy code here

Answer Source

Here is a good example Easy Node Authentication: Setup and Local

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) {

        // find a user whose email is the same as the forms email
        // we are checking to see if the user trying to login already exists
        User.findOne({ '' :  email }, function(err, user) {
            // if there are any errors, return the error
            if (err)
                return done(err);

            // check to see if theres already a user with that email
            if (user) {
                return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
            } else {

                // if there is no user with that email
                // create the user
                var newUser            = new User();

                // set the user's local credentials
          = email;
                newUser.local.password = newUser.generateHash(password);

                // save the user
                    if (err)
                        throw err;
                    return done(null, newUser);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download