How to connect authentication enabled mongodb

I’m getting error like authentication failed error while connecting custom database for login. But provided correct username and password. Anything I’m missing?

With that amount of information it may be somewhat difficult to troubleshoot the situation; you should consider updating the post with additional information:

  • code snippet of the login script;
  • exact error message you get.
  • any other info that you think may be useful.

@jmangelo

My login script:

function login(email, password, callback) {
const mongo = require(‘mongodb’);
const bcrypt = require(‘bcrypt’);

mongo(‘mongodb://username:password@server_ip/db_name’, function (err,db) {

if (err) return callback(err);
const customers = db.collection('customers');

customers.findOne({ email: email }, function (err, customer) {

  if (err) return callback(err);
  if (!customer) return callback(new WrongUsernameOrPasswordError(email));

    bcrypt.compare(password, customer.password, function (err, isValid) {
    
    if (err || !isValid) 
      return callback(err || new WrongUsernameOrPasswordError(email));
    
    return callback(null, {
        email: customer.email,
        user_id: customer._id
    });
    });
  
});

});
}

I’ getting error as Authentication failed … I have enabled authorization for my database, the username and password I’m giving here is correct… still getting this error… Any idea?

Anything I’m missing?

Please help!!

Thanks for sharing the script; the script has several logic paths that due to errors either in configuration or even transient (like network) may lead to a failed authentication attempt.

One possible approach in these situation that generally proves useful would be to instrument the script with console.log statements so that you gain additional insight about the internal execution flow and locate the source of the failure. There’s reference information about this type of troubleshooting at (Troubleshoot Custom Databases).