0

I am creating an OAuth 2.0 service for my up-and-coming Alexa skill, and when I'm getting a client from the clients database using NeDB, there's no callback.

getClient()

module.exports.getClient = async (clientId, clientSecret) => {
    let params = {clientId}
    let result;
    console.log("get client")
    function findClient(params) {
        return new Promise((resolve, reject) => {
            console.log(params)
            clients.loadDatabase()
            console.log('loaded')
            clients.findOne(params, function(err, doc) { // no callback here
                console.log('before reject')
                if (err) return reject(err)
                console.log("callback")
                if (doc.clientSecret) {
                    let secret = doc.clientSecret
                    let id = doc.clientId
                    resolve({clientSecret: secret, clientId: id})
                } else {
                    resolve({clientId: doc.oauth.clientId})
                }
            })
            console.log('does it even exec?')
        })
    }
    console.log(clientSecret)
    if (clientSecret) {
        params.clientSecret = clientSecret
    }
    result = await findClient(params)
    console.log(result)
    return result;
}

Here is the request I'm making:

client_id=1
client_secret=secret123
username=testUser
password=testPass

And finally here is the express route:

app.post('/oauth/authenticate', (req, res, next) => {
    users.findOne({name: req.body.username}, (err, doc) => {
      if (err) throw err;
      req.body.user = doc

      next();
    })
  }, oauth.authorize({
    authenticateHandler: {
      handle: req => {
        return req.body.user
      }
    }
  }))

EDIT: Upon adding a callback when loading the database, I got this error:

loadDatabase Error: More than 10% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss

Thank you!

IceBotYT
  • 74
  • 1
  • 14
  • Souldn't your express route call `getClient`? Or what should be the relation between `getClient` and the shown express callback? – kruschid Apr 13 '21 at 21:24
  • I am using an express wrapper called `express-oauth2-server`. You can check out the package [here](https://npmjs.com/package/express-oauth2-server). – IceBotYT Apr 13 '21 at 21:47
  • does "there is no callback" mean that it neither log `before reject` nor `callback`, is that correct? – kruschid Apr 14 '21 at 10:41
  • Correct. It logs “does it even exec?” but no “callback” – IceBotYT Apr 14 '21 at 11:28
  • nedb readme says: "if loadDatabase fails, all commands registered to the executor afterwards will not be executed..." you could try to log the result of `loadDatabase` with `clients.loadDatabase((err) => console.log("loadDatabase", err))`. Just do make sure that the db is loaded correctly. – kruschid Apr 14 '21 at 11:44
  • Sure. Will do that later today – IceBotYT Apr 14 '21 at 13:13
  • There we go, we got an error, here you go: `loadDatabase Error: More than 10% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss` Maybe I should delete the file and insert my data again? – IceBotYT Apr 14 '21 at 19:06
  • do you have a `beforeDeserialization` hook? if no, then sure, you could try to reset your data. Good luck. – kruschid Apr 14 '21 at 19:53
  • No, I don't have a `beforeDeserialization` hook. I'll reset my data and report back. – IceBotYT Apr 14 '21 at 20:04
  • 1
    Thank you. That worked. – IceBotYT Apr 14 '21 at 20:06
  • cool, happy to help. – kruschid Apr 14 '21 at 20:54

1 Answers1

1

Clearing out my database and inserting the data again fixed it

IceBotYT
  • 74
  • 1
  • 14