0

I am trying to make a roblox mongodb connector, but the connection is not working properly. It says MongoError: bad auth : Authentication failed. whenever I try.

Here is my code

const express = require('express')
const app = require('express')()
const bodyParser = require('body-parser');
const mongoose = require("mongoose")

const username = process.env.USERNAME
const passwordAuth = process.env.PASSWORD

const mongoPass = process.env.MONGO_PASS
const mongoUser = process.env.MONGO_USER

mongoose.connect(`mongodb+srv://${mongoUser}:${mongoPass}@cluster0.i3et1.gcp.mongodb.net/testing?retryWrites=true&w=majority`, {useNewUrlParser: true, useUnifiedTopology: true });

var db = mongoose.connection;

db.on("error", console.error.bind(console, "connection error:"));

db.once("open", function() {
    console.log("Connection To MongoDB Atlas Successful!");
});

app.use(express.json());
app.use((req, res, next) => {  
    const auth = {login: username, password: passwordAuth}

    // parse login and password from headers
    const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
    const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')

    // Verify login and password are set and correct
    if (login && password && login === auth.login && password === auth.password) {
        // Access granted...
        return next()
    }

    res.set('WWW-Authenticate', 'Basic realm="401"')
    res.status(401).send('Authentication required.')
})

I have checked the mongo_user and the mongo_pass many times, and they are exactly identical to my user and pass.

Cmb
  • 334
  • 2
  • 18
  • Have you tried logging the variables to see if it actually getting it from the env correctly? I have a feeling that nothing is getting passed. – beskgar Dec 24 '21 at 00:30
  • Check the mongod log from the server, see if it reports the user auth that you are attempting. (unless this is free tier, then you can't get the log) – Joe Dec 24 '21 at 05:39
  • Make sure the password is URL encoded, if necessary – Joe Dec 24 '21 at 05:41
  • See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Dec 24 '21 at 08:07
  • In reply to the first comment, In my actual code I have the actual username and password in the URL itself – Cmb Dec 24 '21 at 14:05
  • And I am using a free tier. – Cmb Dec 24 '21 at 14:05

0 Answers0