Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i store other form fields with passport-local.js

I am working on a node+passport.js authentication. I make a simple login/signup app. It's working fine but, it stores only username and password.

How can I store the other Form Fields like Phone number, email, hobbies, gender into database through a signup.html page with working login passport authentication? Can anybody have solution for that 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 schema of email, username, password, gender phone and also given fields in signup.html page. but it stores only username and password fields only.........

like image 817
RohanArihant Avatar asked Apr 21 '16 06:04

RohanArihant


People also ask

What is Passport authenticate (' local ')?

passport-local The local authentication strategy authenticates users using a username and password. The strategy requires a verify callback, which accepts these credentials and calls done providing a user. passport-jwt This module lets you authenticate endpoints using a JSON web token.

Should I use Passport js for authentication?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

What does Passport session () do?

To sum it up, passport. serializeUser() saves the user inside the session which was earlier created by express-session middleware. Once the user is saved inside the session, the session itself also will get stored inside the database (only if we opt to).


1 Answers

Open passport.js file ( gernally inside config folder)

find this line of code.

    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

}));
like image 198
Shubham Batra Avatar answered Nov 14 '22 22:11

Shubham Batra