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!