Could you please tell me where is the problem or for what reason I got the authentication problem when I try to connect with mongodb atlas,
This is the error message that I got:
MongoServerError: bad auth : Authentication failed. at Connection.onMessage (C:\Users\lenovo\Desktop\dev\mern-tutorial\node_modules\mongodb\lib\cmap\connection.js:207:30) at MessageStream.<anonymous> (C:\Users\lenovo\Desktop\dev\mern-tutorial\node_modules\mongodb\lib\cmap\connection.js:60:60) at MessageStream.emit (node:events:527:28) at processIncomingData (C:\Users\lenovo\Desktop\dev\mern-tutorial\node_modules\mongodb\lib\cmap\message_stream.js:132:20) at MessageStream._write (C:\Users\lenovo\Desktop\dev\mern-tutorial\node_modules\mongodb\lib\cmap\message_stream.js:33:9) at writeOrBuffer (node:internal/streams/writable:389:12) at _write (node:internal/streams/writable:330:10) at MessageStream.Writable.write (node:internal/streams/writable:334:10) at TLSSocket.ondata (node:internal/streams/readable:754:22) at TLSSocket.emit (node:events:527:28) { ok: 0, code: 8000, codeName: 'AtlasError', [Symbol(errorLabels)]: Set(1) { 'HandshakeError' } } [nodemon] app crashed - waiting for file changes before starting...
I made a .env file that includes the link of mongodb cluster:
MONGO_URI = "mongodb+srv://imen:imen@cluster0.rsyvyrl.mongodb.net/?retryWrites=true&w=majority"
Then in db.js I have this:
const mongoose = require('mongoose')
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI)
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline)
} catch (error) {
console.log(error)
process.exit(1)
}
}
module.exports = connectDB
Finally in my entry point which is server.js I have this:
const express = require('express')
const colors = require('colors');
const connectDB = require('./config/db')
const dotenv = require('dotenv').config()
const port= process.env.PORT || 8000
connectDB()
const app = express()
app.listen(port, ()=> console.log(`starting the server on port: ${port}`))