perfood / couch-auth

Powerful authentication for APIs and apps using CouchDB (or Cloudant) with Node >= 14
MIT License
68 stars 19 forks source link

TypeError: Cannot read property 'user' of undefined #9

Closed sullyinc closed 3 years ago

sullyinc commented 3 years ago

First, thanks for all your work on this library! I've been using it to get auth working quickly with couchdb.

Second, I'm seeing an error when attempting to register a new user with 0.14.0:

(node:79787) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'user' of undefined

To reproduce: I think the relevant parts of my config are:

  local: {
    // Set this to true to disable usernames and use emails instead
    emailUsername: true,
  },

  userDBs: {
    defaultDBs: {
      private: ['user'],
      shared: []
    }
  }

Then, I issue the following request: The request succeeds with 200 OK

% curl -X POST \
  -d '{"email": "test@example.io", "password": "password1", "confirmPassword": "password1"}' \
  -H 'Content-Type: application/json' \
  http://localhost:3001/auth/register

{"success":"Request processed."}           

But looking at the server logs I see the following stacktrace:

POST /auth/register 200 26.512 ms - 32
(node:79787) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'user' of undefined
    at DBAuth.getDBConfig (/[path]/node_modules/@sl-nx/superlogin-next/lib/dbauth/index.js:199:101)
    at /Users/asullivan/Development/spock-express/node_modules/@sl-nx/superlogin-next/lib/user.js:1001:46
    at Array.forEach (<anonymous>)
    at processUserDBs (/[path]/node_modules/@sl-nx/superlogin-next/lib/user.js:1000:20)
    at SuperLogin.addUserDBs (/[path]/node_modules/@sl-nx/superlogin-next/lib/user.js:1018:9)
    at SuperLogin.insertNewUserDocument (/[path]/node_modules/@sl-nx/superlogin-next/lib/user.js:294:30)
    at /[path]/node_modules/@sl-nx/superlogin-next/lib/user.js:258:46
(node:79787) 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: 2)
(node:79787) [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.

Do you know what this could be? I tried clearing everything in sl-users and _users but still no luck.

fynnlyte commented 3 years ago

Hi,

This shouldn't happen if everything is set up correctly.

fynnlyte commented 3 years ago

Ah I think a config entry under userDBs for the model is still expected, eventhough it should not be necessary anymore. Can you try this as a workaround?

 userDBs: {
    defaultDBs: {
      private: ['user'],
      shared: []
    },
   model: {
     user: {}
   }
  }
fynnlyte commented 3 years ago

Can you install this commit and let me know whether it fixed the issue without modifying the config?

npm i github:@sl-nx/superlogin-next#dfc9151
fynnlyte commented 3 years ago

Fixed in the most recent release.