1

I am new to web development.

I am following a Node, Express, Passport and MongoDB tutorial (there is a question about this specific tutorial on SO but I am unable to apply the solution to my problem.)

Passport provisions saving email and password. I want to add a displayName. I have added this attribute to my schema. Added a field for it in the signup form. Then tried to save it via the localStrategy when creating a new User. The email and password save successfully however displayName does not.

in user.js userSchema is defined as

var userSchema = mongoose.Schema({
    local           : {
        email       : String,
        password    : String,
        displayName : String,
    }
});
module.exports = mongoose.model('User', userSchema);

the sign up form is in signup.ejs

<form action="/signup" method="post">
    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="displayName">
        //I want to save this to local.displayName in userSchema
    </div>
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email">
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password">
    </div>

    <button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>

routes.js to process the signup form

app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

passport.js

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,displayName,email,password, done) {
        //asynchronous
        //User.findOne wont fire unless data is sent back
        process.nextTick(function() {

            //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({ 'local.email' : 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
                    newUser.local.email = email;
                    newUser.local.password = newUser.generateHash(password);

                    // i need help with this.
                    newUser.local.displayName = req.body.displayName; 

                    //save the user
                    newUser.save(function(err) {
                        if(err)
                            throw err;
                        return done(null, newUser);
                    });
                }
            });
        });
    }));

I want to save the value entered for displayName in the form to local.displayName at the time of sign up.

I have tried:

newUser.local.displayName = req.body.displayName; did not work

also,

var displayName = req.body.displayName; did not work

I have tried the following solutions to no avail:

Using PassportJS, how does one pass additional form fields to the local authentication strategy?

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

Update or add fields in passport.js local-strategy?

EDIT: console.log output of req.body.displayName and newUser.local.displayName

console.log output

user3890141
  • 111
  • 1
  • 1
  • 10
  • 1
    Try console.log of req.body.displayName. Let me know what it returns. – Rohan Dhar Nov 17 '18 at 09:15
  • console.log(req.body.displayName) outputs the name I enter in the signup form. @RohanDhar – user3890141 Nov 17 '18 at 18:28
  • Ok so turns out it was working to begin with. I use Robo 3T to look at my database and refreshing the connection was not enough to reflect the change. After reestablishing a connection with the host I was able to see the updated db. – user3890141 Nov 17 '18 at 19:28

1 Answers1

-1

Ok, turns out it was working to begin with. I use Robo 3T to look inside my database and refreshing the connection was not enough to reflect the change. After reestablishing a connection with the host I was able to see the updated db.

user3890141
  • 111
  • 1
  • 1
  • 10