next-auth: Mongo Atlas - MongoServerSelectionError: getaddrinfo ENOTFOUND
Your question
What am I missing using Mongo Atlas?
What are you trying to do
- I’m trying to use Google auth
- Whitelisted all IPs on Mongo atlas
- Using env variable gives me:
[next-auth][error][adapter_connection_error] MongoServerSelectionError: getaddrinfo ENOTFOUND cluster0.bixqg.mongodb.net
- I tried to do everything from similar issue https://github.com/nextauthjs/next-auth/issues/314 with no success
- I tried to deploy to
vercel
to run it not locally - the same result
Feedback My code:
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
const options = {
// Configure one or more authentication providers
providers: [
Providers.Google({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET
}),
],
database: process.env.DATABASE_URL,
};
export default (req, res) => NextAuth(req, res, options)
The full error:
[next-auth][error][adapter_connection_error] MongoServerSelectionError: getaddrinfo ENOTFOUND cluster0.bixqg.mongodb.net
at Timeout._onTimeout (/Users/maxim/WebstormProjects/tovarisch/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
name: 'MongoServerSelectionError',
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { 'cluster0.bixqg.mongodb.net:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
[next-auth][error][oauth_callback_handler_error] TypeError: Cannot destructure property `manager` of 'undefined' or 'null'.
- Found documentation but was incomplete
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 30
- Comments: 41 (5 by maintainers)
Commits related to this issue
- add nextauth & google login Pinned typeorm version to work around issue https://github.com/nextauthjs/next-auth/issues/833 — committed to ListKeeper/ListKeeper by npwxx 4 years ago
The issue seems to be related to typeorm version 0.2.29 according to this https://github.com/typeorm/typeorm/issues/7009 .
Adding this to your package.json should fix the issue temporarily until they fix it
"resolutions": { "typeorm": "0.2.28" }
Edit: I forgot to mention, this workaround only works if you are using yarn. Otherwise use @unflawed solution instead
fixed by this command
npm i typeorm@0.2.28
I just switched from mLab to Mongo Atlas, and started having the same problem. I found this thread, and switched from
"typeorm": "^0.2.29"
to"typeorm": "0.2.28"
(note the LACK of a caret ^ in the version, to LOCK it to 0.2.28). That seems to have resolved the issue, and I am now able to connect to Mongo Atlas.If I switch back to 0.2.29 or use any kind of semantic version auto-upgrade option (^ or ~ or .x) in the version, it uses the latest, and also has a problem. Locking the typeorm node module version to 0.2.28 seems to resolve the issue.
not sure if I am the only one, but I haven’t been able to get it working with any method in this thread so far.
Fixed. If you get this, you need to install and import the
reflect-metadata
package.npm install typeorm --save
npm install reflect-metadata --save
reflect-metadata
somewhere in the global place of your app (for example in app.ts):Suggest making this clearer in the docs if possible.
this is available in the latest canary release!
Although it hasn’t been released yet, the PR to fix this in TypeORM has been merged.
I was able to fix it using the following config in
[...nextauth].js
:To resolve for now without any “hackery”, simply bypass the string parser’s reliance on typeorm in the adapter and construct the object yourself. This way you don’t have to install a previous typeorm version, modify your package.json, or include reflect-metadata.
If your string looks like this:
mongodb+srv://<username>:<password>@<cluster>.yu5ue.mongodb.net/<dbname>?retryWrites=true&w=majority
In your options for NextAuth, make your
database
value an object instead of a string. The structure should look as follows:Important: Note that it says uri and not url. Obviously it is recommended to store values in .env.local.
Optional values such as
synchronize
andlogger
can also be added to the object.Source of the issue seems to be coming from typeorm config. Big thanks to Richard Furberg for pointing me in the right direction.
@Zertz @matiastucci Thanks for the update!
Great to know there is a fix in progress with TypeORM!
Will get that rolled in and we can update the dependencies and docs when typeorm@0.3.0 is out.
This worked for me:
yarn add typeorm@next
and adding this in the
package.json
:@andrewgwallace try changing “optionalDependencies” to “peerOptionalDependencies” in your package.json like so:
"peerOptionalDependencies": { "mongodb": "^3.6.2" }
Fix should be available in the latest
typeorm
@0.2.30Yes that seems to have resolved it! Thank you @SelvinM ! Had to install mongodb as a dependency of course.
Yea – my solution stopped working for me as well. 😭 I reverted to downgrading to typeorm 0.2.28 and the resolutions in my package.json. However now I’m getting
UnhandledPromiseRejectionWarning: Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json
I’ve also tried the steps recommended in issue https://github.com/nextauthjs/next-auth/issues/552 of NextAuth. I’ve got a hunch the issue stems from https://github.com/nextauthjs/next-auth/issues/552#issuecomment-671662283 but that’s out of the scope of my knowledge on how to resolve beyond what he suggested.For now, the stability of NextAuth and MongoDB Atlas is not sufficient enough for production use IMO. I’ll keep an eye on this and other tickets for progress.
Just going to note current key settings here where I last had it. If someone spots an issue let me know. Node version: 12.19.0 package.json:
_app.js
import "reflect-metadata";
[…nextauth].jsSolved for me! Thanks @jrista 😃
@iaincollins Could you pin this issue so it’s easier for other
mongodb
users to find? Thanks! 🎉Adding the resolution to my
package.json
resolved the issue for me.@unflawed your solution worked for me, thanks so much! ❤️
Worked for me. Thanks! 👍