Error: MongoError: bad auth: Authentication failed - mongodb

You might be trying to tell me to "change my password" or "remove brackets, these <>"
I have done both. I have made sure that the password I'm using is correct.
Here is the URI:
mongodb+srv://user:pass#notlbot.iclv0tm.mongodb.net/?retryWrites=true&w=majority
no, my username isnt 'user' and my password also is not 'pass'
I don't know what to do from here, though it may be important to note I am trying to connect from a Linodes server. I'm using AWS to host Atlas.
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)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}

Related

How to resolve Deploying error in heroku using mongodb

MongoServerError: bad auth : Authentication failed.
at Connection.onMessage (/Users/harshkataria/Desktop/Tag/node_modules/mongodb/lib/cmap/connection.js:210:30)
at MessageStream.<anonymous> (/Users/harshkataria/Desktop/Tag/node_modules/mongodb/lib/cmap/connection.js:63:60)
at MessageStream.emit (node:events:527:28)
at processIncomingData (/Users/harshkataria/Desktop/Tag/node_modules/mongodb/lib/cmap/message_stream.js:132:20)
at MessageStream._write (/Users/harshkataria/Desktop/Tag/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...
hello i am facing issue while connecting my app in heroku while using mongo auth

MongoServerError: bad auth : Authentication failed for dotenv

when I use dotenv for hiding my password and username this error happens, but if I don't use dotenv the error disappears:
MongoServerError: bad auth : Authentication failed.
at Connection.onMessage (E:\my work\web development by zhankar\assignment\assignment-11-warehouse\warehouse-backend\node_modules\mongodb\lib\cmap\connection.js:203:30)
at MessageStream.<anonymous> (E:\my work\web development by zhankar\assignment\assignment-11-warehouse\warehouse-backend\node_modules\mongodb\lib\cmap\connection.js:63:60)
at MessageStream.emit (node:events:390:28)
at processIncomingData (E:\my work\web development by zhankar\assignment\assignment-11-warehouse\warehouse-backend\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (E:\my work\web development by zhankar\assignment\assignment-11-warehouse\warehouse-backend\node_modules\mongodb\lib\cmap\message_stream.js:28: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:390:28) {
ok: 0,
code: 8000,
codeName: 'AtlasError',
[Symbol(errorLabels)]: Set(0) {}
}
You are likely using dotenv in the wrong way.
Show how are you passing the variables
Basically, this is a minimal example:
#.env file
username=john
password=alice
No spaces around the =
//server.js
require('dotenv')()
//that populates process.env
console.log(process.env.password, process.env.username)
So to be clear, your details are stored in the process.env object.

Heroku app fails to connect to mongodb atlas server (after migration from mlab)

I recently migrated from mlab to mongodb atlas due to its deprecation. I managed to successfully get a connection to the db and tested it with the mongoDB compass but when I add the environment variable to heroku it gives me the following error message:
MongoNetworkError: failed to connect to server [cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017 closed
2020-11-11T23:53:44.726144+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:372:9)
2020-11-11T23:53:44.726144+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726146+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)]
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.emit (events.js:314:20)
2020-11-11T23:53:44.726148+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-11-11T23:53:44.726149+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-11-11T23:53:44.726149+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:97:5)
2020-11-11T23:53:44.726150+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:124:7
2020-11-11T23:53:44.726150+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connect.js:349:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:365:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.emit (events.js:314:20)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:370:12)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)
2020-11-11T23:53:44.812696+00:00 heroku[web.1]: Process exited with status 0
2020-11-11T23:53:44.850554+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-11T23:53:44.854049+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-11T23:53:47.149749+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-11T23:53:49.907225+00:00 app[web.1]:
It seems as if heroku is adding a port to the end of the MONGODB_URI. Does anyone know whats going on?
my config var in heroku is:
MONGODB_URI = mongodb+srv://username:password#cluster-grwbxfbx.e6jtl.mongodb.net/test?retryWrites=true&w=majority
with username and password being replaced by my admin username and password.
Also here is my code for connecting to the db
import mongoose from 'mongoose'
import options from '../config'
export const connect = (url = options.dbUrl, opts = {}) => {
return mongoose.connect(url, { ...opts, useNewUrlParser: true })
}
export const config = {
secrets: {
jwt: 'learneverything'
},
dbUrl: process.env.MONGODB_URI || 'mongodb://localhost:27017/api-design'
}
Let me know if you need me to post anything else

What is this Mongoose CursorNotFound Error?

I have mongoose throwing this error from time to time, it's not stopping the node application from running but I can't find any information about whats causing the error and I would like to fix it.
The link to the repo is here https://github.com/bkawk/divergence2
I have found I can stop the error by adding a limit to the records here
https://github.com/bkawk/divergence2/blob/master/src/db.js#L94
{ MongoError: cursor id 31871209567 not found
at /Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:598:61
at authenticateStragglers (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:516:16)
at Connection.messageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:552:5)
at emitMessageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:309:10)
at Socket.<anonymous> (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:488:15)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:607:20)
name: 'MongoError',
message: 'cursor id 31871209567 not found',
ok: 0,
errmsg: 'cursor id 31871209567 not found',
code: 43,
codeName: 'CursorNotFound' }

Google OAuth2 callback error

Very, very new to user authentication. Looking to allow users to register with Google.
Current status: I'm successfully making it to this step -
(hiding my email(s))
But, when I click an account, I'm getting this error (500) (using longjohn for longer stack traces):
Error
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:95:21
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:132:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.get (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:227:8)
at Strategy.userProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:84:16)
at loadIt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:345:17)
at Strategy.OAuth2Strategy._loadUserProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:360:25)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:168:16
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:209:7
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:134:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.getOAuthAccessToken (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:190:8)
at loaded (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:164:20)
at NullStore.verify (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/state/null.js:9:3)
at Strategy.OAuth2Strategy.authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:210:26)
at attempt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:174:3)
at router (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:317:13)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
---------------------------------------------
at handleOperationCallback (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:454:24)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:490:9
at authenticateStragglers (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:429:16)
at Connection.messageHandler (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:463:5)
at Socket.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:319:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:560:20)
My code surrounding this functionality is:
// routes.js
router.get('/auth/google', authController.google);
router.get('/oauth2callback', authController.googleCallback
);
// passport.js
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "http://localhost:21015/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));
// authController.js
const passport = require('passport');
exports.google = passport.authenticate('google', {
scope: 'https://www.google.com/m8/feeds'
});
exports.googleCallback = passport.authenticate('google', {
failureRedirect: '/login', // if fail, where to go
failureFlash: 'Failed Login!', // if fail, what to flash
successRedirect: '/', // if success, where to go
successFlash: 'You are now logged in!' // if success, what to flash
});
Apparently you can not use an IP address as and end-point, so try to change your callback-url to : http://localhost:21015/oauth2callback
Make sure you have enabled Google+ API in https://console.cloud.google.com.
To use google Auth you have to enable the API in google console. A very common mistake is when you go to Google Developer Console to enable an API for your project, first time for a project it asks you to create Credentials. The problem is after you create credentials the google+ API (or any other APIs) won't be enabled automatically and you should enable it from your project page in google developer console.
STEPS:
Go to Google Console
Select your project from the top nav (if you didn't yet)
Click on Enable APIS and Services
In the search box search for Google Plus
Select the first result , Google+ API
Click Enable
If you don't see the Enable button and see the 'Create Credentials' button,it means you didn't create credentials yet and follow the instructions on the page to create one. Don't forget to enable API after creating credentials.
For more information you can read this instruction from google Enable and disable APIs
Besides what mentioned above, you don't need to use an absolute URL for your callback and simply you can use a relative one like: /auth/google/callback
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));