Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regarding pull request #84 ( alternative fields for login) #97

Open
deveshsinghal22 opened this issue Aug 27, 2015 · 2 comments
Open

Regarding pull request #84 ( alternative fields for login) #97

deveshsinghal22 opened this issue Aug 27, 2015 · 2 comments

Comments

@deveshsinghal22
Copy link

i have added the new field for usernameQueryFields

UserSchema.plugin(passportLocalMongoose,{
        'usernameField' : 'mobile',
        'usernameQueryFields' : ['email']
    }
);

This is working fine for login but at the time of signup using the above config it is throwing error
BadRequestError: Field mobile is not set when i pass the {email : '[email protected]'} in user. register function.

In my application flow, user have only 2 fields in signup form. one for email/mobile and another for password. if user enter a email then it goes to the email field of userSchema otherwise it goes to mobile field.

@deveshsinghal22
Copy link
Author

I was asking for something like this

    schema.statics.register = function(user, password, cb) {
        // Create an instance of this in case user isn't already an instance
        if (!(user instanceof this)) {
            user = new this(user);
        }

        // check each username query field
        var usernameFieldFound = false,
            usernameField;
        for (var i = 0; i < options.usernameQueryFields.length && !usernameFieldFound; i++) {
            if (user.get(options.usernameQueryFields[i])) {
                usernameField = options.usernameQueryFields[i];
                usernameFieldFound = true;
            }
        }

        if(!usernameFieldFound) {
            //@todo need to refactor this
            return cb(new BadRequestError(util.format(options.missingUsernameError, options.usernameField)));
        }

        /*if (!user.get(options.usernameField)) {
            return cb(new BadRequestError(util.format(options.missingUsernameError, options.usernameField)));
        }*/

        var self = this;
        self.findByUsername(user.get(usernameField), function(err, existingUser) {
            if (err) { return cb(err); }

            if (existingUser) {
                return cb(new BadRequestError(util.format(options.userExistsError, options.usernameField, user.get(options.usernameField))));
            }
            user.setPassword(password, function(err, user) {
                if (err) {
                    return cb(err);
                }

                user.save(function(err) {
                    if (err) {
                        return cb(err);
                    }
                    cb(null, user);
                });
            });
        });
    };

@DeveloperUX
Copy link

I'm having the same issue. Looks like it works fine for the default 'username' field, but not for a custom field.

I'm using Passport-Local-Mongoose v1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants