How can I store other form fields with passport-local.js

I am working on node + passport.js authentication. I am making a simple login / registration application. It works fine, but it only stores username and password.

How can I store other form fields, such as phone number, email address, hobby, gender in the database through the signup.html login authentication page? Anyone has a solution for this, so I can store all the fields in the database ....

//my schema is :--
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = mongoose.Schema({
    local            : {
        username     : String,
        gender       : String,
        phone        : String,
        email        : String,
        password     : String
    }
 });
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
};


var User = mongoose.model('user', userSchema);

module.exports = User;

In this code, I use the email scheme, username, password, gender phone, and also the field data on the signup.html page. but it only saves username and password fields only .........

+4
3

passport.js( )

.

    passport.use('local-signup', new LocalStrategy({   // 'login-signup' is optional here   
    usernameField : 'email',
    passwordField : 'password',        
    passReqToCallback : true },function(req, email, password, done) {
   var gender = req.body.gender;
  var username = req.body.username;
  var phone = req.body.phone;
 // Now you can access gender username and phone

}));
+6

passReqToCallback req.body:

passport.use(new LocalStrategy({ 
  passReqToCallback: true 
}, function (req, username, password, cb) {
  // Form fields are in req.body if using body-parser
  // ...
});
0

. . passport.js :

       module.exports = function(passport) {
              var criteria;
              passport.use(
               new LocalStrategy({ usernameField: 'username' }, (username, password, done) => {
               if(username.indexOf('@') > -1) { 
               criteria = {
                    email: username,
                };
               } else {
                criteria = {
                    mobile: username,
                };
              }

                // Match user
                User.findOne(criteria).then(user => {
                  if (!user) {
                      return done(null, false, {
                        success: null,
                        errors: "User is not registered",
                        result:null
                    });
                }

                // Match password
                bcrypt.compare(password, user.password, (err, isMatch) => {
                    if (err) throw err;
                    if (isMatch) {
                        return done(null, user);
                    } else {
                        return done(null, false, { 
                            success: null,
                            errors:'Password incorrect',
                            result: null
                         });
                    }
                });
            });
        })
    );
    enter code herepassport.serializeUser(function(user, done) {
        done(null, user.id);
    });

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

All Articles