How to fix the error 'MongoServerError: bad auth : Authentication failed'?
Tags: mongodb, nosql, errors, solution
Published: 2022-11-07 11:48:17

Oftentimes, you might come across the error "**bad auth: authentication failed.**" What does this error mean? 

"**Bad auth**" means the authentication method is not proper due to providing wrong **username** or **password**; hence, it failed.

This blog will discuss the possible mistakes we make for which this error appears and the solutions we can try to fix the error.

1) **using whitespaces**

When you put your username and password, **do not add any whitespace (extra space) before and after** your username and password in the MongoDB string connection. 

**Mistake-1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r7arfoi1ub45k9gsg636.png)

**Mistake-2**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6antmp1l3s0hmyzq3r1.png)

**Correct way-1** 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t96lox3fe8a7ts0ot75a.png)

> const uri = `mongodb+srv://iphoneUser:Tq6uuoPOts7CiRlK@cluster0.ebhzh.mongodb.net/?retryWrites=true&w=majority`;

**Correct way-2** 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n55vx3kcs8qm4mz1xiv.png)

> const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.ebhzh.mongodb.net/?retryWrites=true&w=majority`;


2) **username and password did not match**

Check if the username and password in your **MongoDB connection string** matches with the username and password in your **mongodb database access**

**Correct:** Username and password matches

**MongoDB connection string**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9v4j0yz7qpccl9deix2.png)

**Database Access**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vizn02k3lmw9wfp6zwg.png)


3) **Forgot to save username and password**

Make sure you have clicked the **'Add User'** button after creating the username and password in your database access.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72c9imocnrwdpo3l8g6i.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0la8smytopau7aho6ra.png)


4) **Using angle brackets**

Do not put your username and password inside the **angle brackets** like the picture below:

**Wrong**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gxkr4r5bre9iiqnqi1d.png)

**Correct**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qe5t2f2kz9u9y91z4jxk.png)

5) **Environment variables did not match** 

If you are using environment variables, make sure you have written them correctly in your MongoDB connection string

**Did not match**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkdfxd0kstxxg7g5kg85.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tul7de254hbkx4oq4gy.png)

**Matched**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vjz4ijrcamm0sdn27p0z.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0m0xitj2d15utsl3gbi.png)

6) **Do not add any space when assigning your secret information to the environment variables**

Wrong:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzjfd63y391hv59xvm5y.png)

Correct:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvt92bj9eq4g9xt6vn1u.png)

7) **Don't forget to do the following before using the environment variables**

Always check if you have installed **dotenv** in your package.json. If you haven't, then run this command to install **dotenv**

`npm i dotenv`

After installation, make sure you have imported dotenv

`require("dotenv").config();`

Take time and go through your code to see if you have made the mistakes discussed above when your authentication fails. I am sure one of these solutions will help you connect your MongoDB successfully. Happy coding!!!