# How to connect authentication enabled mongodb

**URL:** <https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174>\
**Category:** Get Help\
**Tags:** login, auth0\
**Created:** [March 6, 2019, 7:23am UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174 "2019-03-06T07:23:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![poojak](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/poojak/32/11406_2.png) [@poojak](https://community.auth0.com/u/poojak)\
**Post date:** [March 6, 2019, 7:23am UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174/1 "2019-03-06T07:23:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [March 6, 2019, 2:11pm UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174/2 "2019-03-06T14:11:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![poojak](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/poojak/32/11406_2.png) [@poojak](https://community.auth0.com/u/poojak)\
**Post date:** [March 11, 2019, 7:48am UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174/3 "2019-03-11T07:48:07Z")

</div>

@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!!

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [March 11, 2019, 11:23am UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174/4 "2019-03-11T11:23:01Z")

</div>

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](https://auth0.com/docs/connections/database/custom-db/error-handling#troubleshooting-errors)).

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [August 30, 2019, 11:04am UTC](https://community.auth0.com/t/how-to-connect-authentication-enabled-mongodb/22174/5 "2019-08-30T11:04:37Z")

</div>


