Using "Username" field in Jwt and passport

I am using jwt using node.js and passport.js. I can make the authentication work if I use the email as the username. But if I use the actual username, I cannot make it work. It gives me an error of 'Email is not registered. Below is how I implement it:

passport.js

passport.use(
    new localStrategy({ usernameField: 'email' },
        (username, password, done) => {
            User.findOne({ email: username },
                (err, user) => {
                    if (err)
                        return done(err);
                    // unknown user
                    else if (!user)
                        return done(null, false, { message: 'Email is not registered' });
                    // wrong password
                    else if (!user.verifyPassword(password))
                        return done(null, false, { message: 'Wrong password.' });
                    // authentication succeeded
                    else
                        return done(null, user);
                });
        })
);

user.model.js

var userSchema = new mongoose.Schema({
    firstName: {
        type: String,
        required: 'First name can\'t be empty'
    },
    middleName: {
        type: String,
    },
    lastName: {
        type: String,
        required: 'Last name can\'t be empty'
    },
    userName: {
        type: String,
        required: 'Username can\'t be empty'
    },
    email: {
        type: String,
        required: 'Email can\'t be empty',
        unique: true
    },
    password: {
        type: String,
        required: 'Password can\'t be empty',
        minlength : [4,'Password must be atleast 4 character long']
    },
    saltSecret: String,
    isActivated: {
        type: Boolean,
    },
    dateAdded: {
        type: Date
    }
});

user.controller.js

module.exports.authenticate = (req, res, next) => {
    // call for passport authentication
    passport.authenticate('local', (err, user, info) => {
        // error from passport middleware
        if (err) return res.status(404).json(err);
        // registered user
        if (user) return res.status(200).json({ "token": user.generateJwt() });
        // unknown user or wrong password
        else return res.status(401).json(info);
    })(req, res);
}

I need to use the "userName" and "password" fields for my authentication instead of the email. I am using the email because it is what is being used in the tutorial that I am following. Thank you.

Below is how I try to implement to get what I need but it doesn't work:

passport.use(
    new localStrategy({ usernameField: 'userName' },
        (username, password, done) => {
            User.findOne({ userName: username },
                (err, user) => {
                    if (err)
                        return done(err);
                    // unknown user
                    else if (!user)
                        return done(null, false, { message: 'Useris not registered' });
                    // wrong password
                    else if (!user.verifyPassword(password))
                        return done(null, false, { message: 'Wrong password.' });
                    // authentication succeeded
                    else
                        return done(null, user);
                });
        })
);

1 answer

  • answered 2019-05-15 04:23 Amol B Jamkar

    Try this

     passport.use(
        new localStrategy({ usernameField: 'userName' },
            (username, password, done) => {
                User.findOne({ userName: username},
                    (err, user) => {
                        if (err)
                            return done(err);
                        // unknown user
                        else if (!user)
                            return done(null, false, { message: 'Email is not registered' });
                        // wrong password
                        else if (!user.verifyPassword(password))
                            return done(null, false, { message: 'Wrong password.' });
                        // authentication succeeded
                        else
                            return done(null, user);
                    });
            })
    )