Node.js MongoError: auth failed - node.js

I found out my website wasn't working anymore because i can't auth to my mongolab DB.
here is my code (it used to work for months):
var mongoose = require('mongoose');
mongoose.connect('mongodb://USER:PASSWORD#ds053419.mongolab.com:53419/jobinfest',
function(err) {
console.log(err);
if (err) { throw err; }
}
);
I have this error:
{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
But i can access the db via mongolab and :
mongo ds053419.mongolab.com:53419/jobinfest -u USER -p PASSWORD
I tried to create new users, even a new db. This way of connection seems to not work anymore

In your code be sure you are replacing USER:PASSWORD with your actual username and password.
mongodb://USER:PASSWORD#ds053419.mongolab.com:53419/jobinfest

Related

Failed to connect node.js app to MongoDB Atlas despite using the correct connection credentials

I'm trying to connect my node.js application to MongoDB Atlas but I keep getting a "Bad authentication error" and yes, I am using the current database user credentials.
Here is the snippet that's supposed to connect to MongoDB Atlas
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
console.log('MongoDB Connected: ' +conn.Connection.host)
}catch (err) {
console.error(err)
process.exit(1)
}
}
My terminate shows me bad authentication and some key-pairs that look so:
{
ok: 0,
code: 8000,
codeName: 'AtlasError',
name: 'MongoError'
}
Any ideas why it is not connecting to MongoDB Atlas?
I finally singled out the problem, it was the MongoDB connection string. I was simply inserting my password in the password field without removing the angle brackets.

Unhandled rejection error: password authentication failed for user "user"

I have a postgres datatbase set up and I installed PostgreSQL and knex.js in my node.js server file and added the code to connect to my database but everytime I use postman to see if im able to add users I get this error message
const db= knex
({
client
:'pg',
connection:{
host:'127.0.01',
user:'',
password:'',
database:'smartbrain'
}
});
app.post('/register',(req,res)=>{
const {email,name,password}=req.body;
db('user').insert({
email:email,
name:name,
joined:new Date()
})
.then(console.log())

Redis-node : Ready check failed: NOAUTH Authentication required

I am using the redis package on npm after installing the redis from the digital ocean guide I secured it using one of their post. I set the requirepass to a password and then logged into my redis-cli and authenticated myself successfully. But I am unable to do the same from my Node.js application.
This is my config in Node
client = redis.createClient({
no_ready_check: true,
host : 'ip address',
port : port
});
client.auth('secretPassword', function(err, doc){
if(err)
throw err;
else if(doc === "OK"){
console.log("Authenticated");
}
});
client.on('error' , function (err) {
console.log("Error");
});
After I run it. I get this error
events.js:160
throw er; // Unhandled 'error' event
^
ReplyError: Ready check failed: NOAUTH Authentication required.
at parseError (/home/ubuntu/demo/vehico-workers/node_modules/redis-parser/lib/parser.js:181:12)
at parseType (/home/ubuntu/demo/vehico-workers/node_modules/redis-parser/lib/parser.js:291:14)
From my best guess, the node-redis driver doesn't implement it properly as I am able to authenticate from cli but not from Node application. Anybody who is running a secure redis.conf on AWS EC2 successfully?
I have the same probleme, and didnt find any solution on the Internet.
I change the directive pass to password in setting.js
redis: { host: "192.168.x.xx", password: '******', port: 6379 },
storageModule: require("node-red-contrib-redis-storage"),
And it work well.

Mongolab connection not working properly

I have a simple NodeJS service which uses mongodb. The thing is now I am "migrating" to mongolab insted of a local mongodb. The connection is working properly but when my service trys to retrieve some data, the following error apears:
{ [MongoError: auth failed]
name: 'MongoError',
message: 'auth failed',
ok: 0,
errmsg: 'auth failed',
code: 18 }
I've check some similar problems which and discarded the following isues:
Is not the mongolab URL.
Is not the mongolab database user (I am using admin/admin which has dbOwner role).
Server connection:
var db = mongojs('mongodb://admin:admin#xxxxx.mongolab.com:xxxxx/database', ['players']);
Service
app.get('/player', function (req, res) {
db.players.find().sort({_id:-1},function(err,docs) {
if (err) {
console.log(err);
}
res.json(docs);
});
});

Problems connecting to MongoDB server from MongoLab

I'm trying to connect to mongodb created by MongoLab, but it always seems to fail.
var mongoose = require('mongoose');
mongoose.connect('mongodb://<dbuser>:<dbpassword>#ds123854.mongolab.com:12345/the_db');
mongoose.connection.on('open', function (ref) {
console.log('Connected to mongo server.');
});
mongoose.connection.on('error', function (err) {
console.log('Could not connect to mongo server!');
console.log(err);
});
// check if mongoose connected; 0 = no; 1 = yes; 2 = connecting; 4 = disconnecting
console.log("mongoose connection: " + mongoose.connection.readyState);
I get the log:
mongoose connection: 2
Could not connect to mongo server!
{ [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
I tried to check the password, so I did the following in my console:
> mongo ds123854.mongolab.com:12345/the_db -u <dbuser> -p <dbpassword>
MongoDB shell version: 3.0.7
connecting to: ds123854.mongolab.com:12345/the_db
rs-ds123854:PRIMARY> db.mynewcollection.insert({ "foo": "bar"})
WriteResult({ "nInserted" : 1 })
rs-ds123854:PRIMARY> db.mynewcollection.find()
{ "_id" : ObjectId("563c1913504f0ab5cb96d74c"), "foo" : "bar" }
The username and password seem right and I can see that the insert command did put something into the database.
I am hosting my server in localhost, so I believe that's the problem. What sort of things am I missing in my configuration?
Solution: Ensure that your version of Mongoose is up to date.
In my case, I was using version 3.6.13, but the current version is 4.2.4
If you specified your mongoose version, just update your package.json file and use command line npm update

Resources