nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.61k stars 3.46k forks source link

Error connecting to Atlas database using provided example #1108

Closed timsoraro closed 3 years ago

timsoraro commented 3 years ago

I'm not sure if it's a problem with next-auth or my configuration. I'm trying to implement this example repo https://github.com/nextauthjs/next-auth-example using mongodb.

I setup a mongo database on Atlas. These are my files:

.env

MONGO_URI=mongodb+srv://<username>:<password>@ xxxxxx.xxxxxx.mongodb.net/<dbname>
EMAIL_SERVER_HOST= emailhost
EMAIL_SERVER_PORT=emailport
EMAIL_SERVER_USER=apikey
EMAIL_SERVER_PASSWORD=emailserverpassword
EMAIL_FROM=email

Of course, I changed the real values.

/pages/api/[...nextauth].js

import NextAuth from "next-auth";
import Providers from "next-auth/providers";

const options = {
  providers: [
    Providers.Email({
      server: {
        host: process.env.EMAIL_SERVER_HOST,
        port: process.env.EMAIL_SERVER_PORT,
        auth: {
          user: process.env.EMAIL_SERVER_USER,
          pass: process.env.EMAIL_SERVER_PASSWORD,
        },
      },
      from: process.env.EMAIL_FROM,
    }),
  ],
  database: {
    type: "mongodb",
    uri: process.env.MONGO_URI,
    w: "majority",
    useNewUrlParser: true,
    useUnifiedTopology: true,
    retryWrites: true,
    synchronize: true
  },
};

export default (req, res) => {
  console.log(process.env.MONGO_URI)
  NextAuth(req, res, options)
}

When running 'npm run dev` I get to the login page but when I submit an email address the browser stuck loading and it prints this:

mongodb+srv://<username>:<password>@ xxxxxx.xxxxxx.mongodb.net/<dbname>
API resolved without sending a response for /api/auth/session, this may result in stalled requests.

and after some time the console then prints this:

https://next-auth.js.org/errors#adapter_connection_error
[next-auth][error][session_error] TypeError: Cannot destructure property 'manager' of 'connection' as it is null.
    at Object.<anonymous> (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:102:9)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:28:103)
    at _next (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:30:194) 
https://next-auth.js.org/errors#session_error
[next-auth][error][adapter_connection_error] MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at Timeout._onTimeout (/Users/user/Downloads/nextauth-examples-master/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { '127.0.0.1:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
} 
https://next-auth.js.org/errors#adapter_connection_error
(node:66120) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'manager' of 'connection' as it is null.
    at Object.<anonymous> (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:102:9)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:28:103)
    at _next (/Users/user/Downloads/nextauth-examples-master/node_modules/next-auth/dist/adapters/typeorm/index.js:30:194)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:66120) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:66120) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I tried many things but nothing helped, I'll appreciate your help,

Thanks

timsoraro commented 3 years ago

I somehow solved the problem by adding:

const options = {
  site: process.env.NEXTAUTH_URL,
balazsorban44 commented 3 years ago

Strange. @timsoraro which version of next-auth are you using? Since version 3, options.site has no meaning.

timsoraro commented 3 years ago

I'm using "^3.1.0".

timsoraro commented 3 years ago

Okay, you are right, it's not that. I'm not really sure what solved it.

timsoraro commented 3 years ago

Okay I noticed that this will cause the problem I reported orginially:

database: {
    type: "mongodb",
    uri: process.env.MONGO_URI,
    w: "majority",
    useNewUrlParser: true,
    useUnifiedTopology: true,
    retryWrites: true,
    synchronize: true
  },

Which is solved by:

database: process.env.MONGO_URI,

But i got ot say it didn't work when I tried before (so maybe something in my URI got fixed?).

centerjustified commented 3 years ago

I am seeing the same issue with mongoDB hosted on Atlas. @timsoraro does your URI just mongodb+srv://user:password@xxxx.xxx.mongodb.net/db_name or are you also including any other flags? Because that doesn't seem to be solving it for me.

timsoraro commented 3 years ago

@centerjustified yep, just like that without any flags. Did you try using a new user that is not the admin (just throwing some ideas here)?

centerjustified commented 3 years ago

@timsoraro originally I was using an admin user. Created a new next-auth user with read/write access to just the one database. That didn't solve the issue. I appreciate you throwing out ideas. Are you using the same user for your general data connections and your auth connections?

timsoraro commented 3 years ago

Yes. Throwing more ideas:

  1. I also did npm cache clean --force.
  2. I just noticed something weird, migrating the code to a different project it didn't work again until I deleted 2 passport packages in package.json. So maybe make sure you don't have conflicted packages?
EnriqueL8 commented 3 years ago

I'm also getting this issue, could we reopen the issue? I've tried:

Any other ideas @timsoraro @centerjustified ? Thanks!