Akryum / vue-summit-app

Example App: Vue + GraphQL + Apollo (Devfest Nantes 2017)
https://devfest-summit.now.sh/
181 stars 24 forks source link

How to get authed connection to the MongoDB? #1

Closed Trellian closed 6 years ago

Trellian commented 6 years ago

Hi @Akryum ,

Thank you for this amazing example!

I have setup all the components for GOOGLE_CLIENT, ENGINE_KEY etc, but I am having trouble configuring for auth on the MongoDB. I get the following error:

(node:14424) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Authentication failed.

I'm not a MongoDB user, sorry! (I'm an Oracle/MySQL sort of guy). I have the mongod running. Can you perhaps let me know what to do next to get auth working? I have been through the MongoDB docs, but they are clear as mud to me on the auth side.

Many thanks, Adrian

Akryum commented 6 years ago

Enable authentication (docs) and create a user (docs) with access to your mongo database. Then, setup the MONGO_URL environment variable like this: mongodb://<user>:<password>@<host>:<port>/<database>.

Trellian commented 6 years ago

Thanks @Akryum I did that, and tested that I could login from the cmd line. when run yarn run dev, i now get the message:

... MongoError: connection 0 to localhost:27017 timed out

Sorry, I know this isn't really your problem, but I'd appreciate the help. TIA

Akryum commented 6 years ago

What URL did you set?

Trellian commented 6 years ago

MONGO_URL=mongodb://iwall:testpw@localhost:27017/admin

Trellian commented 6 years ago

I did try creating a new db 'devfest', with use devfest but when I come to show databases, the db is not there, only 'admin' and local', and I can't connect with MONGO_URL=mongodb://iwall:testpw@localhost:27017/devfest after that either

Akryum commented 6 years ago

did try creating a new db 'devfest', with 'use devfest but when I come to show databases, the db is not there, only 'admin' and local', and I can't connect with

This is fine, Mongo won't create the database until there is something on it.

Akryum commented 6 years ago

Try with 127.0.0.1 instead of localhost.

Trellian commented 6 years ago

ok, just tried that, the connection is still timing out with both 127.0.0.1:27017/admin and 127.0.0.1:27017/devfest.

If I try with a known bad password, I get an auth error, so I know it's at least talking to mongodb.

Akryum commented 6 years ago

Try changing the connection timeout here with the connectTimeoutMS option (docs).

Trellian commented 6 years ago

I tried to edit there, and also from the cmd line, to set the connection string as follows:

MONGO_URL=mongodb://iwall:testpw@127.0.0.1:27017/admin/?connectTimeoutMS=600000

but it doesn't like the syntax as per the docs...

(node:7180) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Illegal trailing backslash after database name

[EDIT] apologies, I've now removed the '/' before the '?', and it accepts that, but still times out at 60secs

Akryum commented 6 years ago

MongoClient.connect(MONGO_URL, { connectTimeoutMS: ... }, (err, result) => {

Trellian commented 6 years ago

You are correct there... sorry, the docs led me astray a bit. I have now got:

    ...MongoClient.connect(MONGO_URL, {connectTimeoutMS: 60000},(err, result) => {...

and the timeout is still there, happening at 60000ms. I think I am wasting your time... this is a silly db connect issue, when we should be talking about the technical details of this repository.

Trellian commented 6 years ago

some more info: The output from the mongo daemon:

2017-11-17T13:49:09.182+0200 I NETWORK  [conn13] received client metadata from 127.0.0.1:64804 conn13: { driver: { name: "nodejs", version: "2.2.33" }, os: { type: "Windows_NT", name: "win32", architecture: "x64", version: "10.0.10586" }, platform: "Node.js v8.4.0, LE, mongodb-core: 2.1.17" }
2017-11-17T13:49:09.203+0200 I -        [conn13] end connection 127.0.0.1:64804 (1 connection now open)

It looks like the connection is being accepted and then closed a few ms later. I'm going to try adding autoReconnect: true

Trellian commented 6 years ago

Aha! A new error, it looks like it's a rights issue:

2017-11-17T13:53:15.109+0200 I ACCESS   [conn14] Successfully authenticated as principal iwall on admin
2017-11-17T13:53:15.120+0200 I ACCESS   [conn14] Unauthorized: not authorized on admin to execute command { createIndexes: "user", indexes: [ { name: "userId_1", key: { userId: 1 } } ] }
MongoError: not authorized on admin to execute command { insert: "system.indexes", documents: [ { ns: "admin.user", key: { userId: 1 }, name: "userId_1", unique: false } ], ordered: true }

which is strange... I added the 'iwall' user as follows:

db.createUser(
  {
    user: "iwall",
    pwd: "testpw",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Trellian commented 6 years ago

ok... I removed that user, recreated it in the 'devfest' db, and the error is now different again, but at least the timeout error is gone!

I'm using MONGO_URL=mongodb://iwall:testpw@127.0.0.1:27017/devfest

and the error I now get is:

TypeError: OAuth2Strategy requires a clientID option

I'm using MongoDB version v3.4.10. Perhaps the auth strategy has changed.

[EDIT] - Ok this is likely to be a google client auth error, which is then clearly my fault. Please excuse my wasting your time, but thanks for all the help so far @Akryum . I'll see what I can do from here.

Trellian commented 6 years ago

@Akryum , Thanks for all your help, it's all working now!

I had GOOGLE_DEFAULT_CLIENT_ID instead of GOOGLE_CLIENT_ID in my env. I'm leaving this here for others that may have had the same problems.

Thanks again for the most complete, professional example of Vue and Apollo available out there!