[Fixed] Error Message: MongoError: bad auth Authentication failed through URI string – Node.js

Photo of author
Written By M Ibrahim
mongodb mongoose node.js

Quick Fix: Make sure you are using the correct password for authentication. Use the user password and not the account password.

The Problem:

A user named ‘david’ is trying to connect to a MongoDB server using a connection string provided by MongoDB. The connection string includes the user’s password. When the user attempts to connect using the Mongoose library, they receive a "MongoError: bad auth Authentication failed" error. The user is seeking assistance in understanding the cause of the error and potential solutions.

The Solutions:

Solution 1: Fix the Confusion between Account Password and User Password

The error message "MongoError: bad auth Authentication failed" typically occurs due to incorrect credentials. In this case, you might be confusing the MongoDB account password with the user password. Make sure you use the user password instead of the account password when connecting to your MongoDB server.

Solution 2: Remove angle brackets from the password

In the provided connection string, the password is enclosed in angle brackets ("<" and ">"). However, these angle brackets should not be included when specifying the password. Remove the angle brackets from the password in the connection string.

Here’s the corrected connection string:

const db = 'mongodb+srv://david:[email protected]/test?retryWrites=true';

Make sure to replace “password” with your actual password.

Now, when you run your code, you should no longer receive the “MongoError: bad auth Authentication failed” error.

Solution 3: Mistake in Password in Connection String

The error “MongoError: bad auth Authentication failed” indicates an incorrect password provided in the connection string. The password used in the connection string should be the cluster password and not the login password. Follow the steps below to reset the cluster password and resolve the issue:

  1. Navigate to the MongoDB Atlas page and select “Database Access” from the left navigation panel.
  2. Select your username and click on the “Edit” button on the right side.
  3. Click on “Change Password.”
  4. Enter a new password, ensuring it contains only alphabetical characters as special characters require encoding. Click “Update User.”

Once the cluster password has been reset, use the new password in your connection string to connect to the MongoDB server. This should resolve the authentication error.

Database Access page in MongoDB Atlas

Solution 4: Use Credentials Separately

To resolve the “MongoError: bad auth Authentication failed” error, separate the authentication credentials from the URI. Instead of including the username and password in the URI, use separate fields for them in the connection options object.

Here’s an example:

mongoose.connect(
  'mongodb+srv://clusterAnything.mongodb.net/test?retryWrites=true&w=majority',
  { 
    user: process.env.MONGO_USER, 
    pass: process.env.MONGO_PASSWORD, 
    useNewUrlParser: true, 
    useUnifiedTopology: true,
  }
);

In this example, the username and password are stored in environment variables named process.env.MONGO_USER and process.env.MONGO_PASSWORD, respectively. This approach enhances security by keeping the credentials separate from the URI, which is visible to anyone with access to the code.

Always ensure that the environment variables containing the credentials are securely stored and not exposed in the code.

Solution 5: Three Quick Tips to Resolve Auth Failed

When encountering the “bad auth” error in MongoDB, consider the following tips:

  1. Check the Password:

    • Ensure you’re using the correct password. Confirm that you’re utilizing the password associated with your database user, not your general Mongo account.
    • URL-encode any special characters in your password. For instance, "p@ssword" should be written as "p%40ssword."
  2. Reset Forgotten Passwords:

    • If you’ve forgotten your database user’s password:
      • Navigate to "Database Access" (assuming you’re using Mongo Atlas).
      • Select your database user.
      • Click "Edit."
      • Generate a new password.
      • Don’t forget to click "Update User" to save the changes.
  3. Avoid Storing Passwords in Plain Text:

    • For security purposes, refrain from storing passwords directly in your code. Explore alternative methods mentioned here to maintain the security of your database access.