Custom Databse for MongoDB doesn't check for username

I added a mongodb custom database to my auth0 connection. It works well however doesn’t check against the username, just the email,
I attempted to edit the code as following, but get a callback is not a function error,
function login(email, username, password, callback) {
const bcrypt = require(‘bcrypt’);
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;
const client = new MongoClient(‘mongodb+srv://mern:’+configuration.MONGO_PASSWORD+’@cluster0-2uci2.mongodb.net/?retryWrites=true&w=majority’);

client.connect(function (err) {
if (err) return callback(err);

const db = client.db('test');
const users = db.collection('users');

users.findOne({$or: [ {email: email }, {username: username }] }, function (err, user) {
  if (err || !user) {
    client.close();
    return callback(err || new WrongUsernameOrPasswordError(email));
  }

  bcrypt.compare(password, user.password, function (err, isValid) {
    client.close();

    if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));

    return callback(null, {
        user_id: user._id.toString(),
        nickname: user.nickname,
        email: user.email
      });
  });
});

});
}

Could someone please help? this is the original format the custom mongodb connection comes in:
function login(email, password, callback) {
const bcrypt = require(‘bcrypt’);
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;
const client = new MongoClient(‘mongodb://user:pass@localhost’);

client.connect(function (err) {
if (err) return callback(err);

const db = client.db('db-name');
const users = db.collection('users');

users.findOne({ email: email }, function (err, user) {
  if (err || !user) {
    client.close();
    return callback(err || new WrongUsernameOrPasswordError(email));
  }

  bcrypt.compare(password, user.password, function (err, isValid) {
    client.close();

    if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));

    return callback(null, {
        user_id: user._id.toString(),
        nickname: user.nickname,
        email: user.email
      });
  });
});

});
}

Hello, @raalrwai! Welcome to the Auth0 Community.

As stated in our documentation, if you have the Require Username option, you might get either the username or the email as the email value. The Get User script should be able to handle both scenarios, and will not consistently produce one or the other.

Let me know if this helps.

Thanks!

1 Like

Hi Jose,
I have custom database enabled for the MongoDB sign on, I also changed the code back to the default template so it is in the original state that you’re referring to which will check for username or email for the email parameter, however I have tried logging in several times, and no matter what I cannot sign in with the username, I know the password works because when I add the @email.com part to the user name it works:
Example:
username: test
email: test@gmail.com
yet only the email one works.