Server: Use ES6 promise for mongoose/node-oatuh2-server

This commit is contained in:
Chocobozzz 2016-07-27 21:15:07 +02:00
parent f24c4e4462
commit 32bb415602
4 changed files with 9 additions and 9 deletions

View File

@ -23,6 +23,7 @@ const database = {
} }
function connect () { function connect () {
mongoose.Promise = global.Promise
mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
mongoose.connection.on('error', function () { mongoose.connection.on('error', function () {
throw new Error('Mongodb connection error.') throw new Error('Mongodb connection error.')

View File

@ -62,7 +62,7 @@ function revokeToken (token) {
} }
function saveToken (token, client, user) { function saveToken (token, client, user) {
logger.debug('Saving token for client ' + client.id + ' and user ' + user.id + '.') logger.debug('Saving token ' + token.accessToken + ' for client ' + client.id + ' and user ' + user.id + '.')
const tokenObj = new OAuthToken({ const tokenObj = new OAuthToken({
accessToken: token.accessToken, accessToken: token.accessToken,
@ -73,13 +73,12 @@ function saveToken (token, client, user) {
user: user.id user: user.id
}) })
return tokenObj.save(function (err, tokenCreated) { return tokenObj.save().then(function (tokenCreated) {
if (err) throw err // node-oauth2-server library uses Promise.try
tokenCreated.client = client tokenCreated.client = client
tokenCreated.user = user tokenCreated.user = user
return tokenCreated return tokenCreated
}).catch(function (err) {
throw err
}) })
} }

View File

@ -29,5 +29,5 @@ function loadFirstClient (callback) {
} }
function getByIdAndSecret (id, clientSecret) { function getByIdAndSecret (id, clientSecret) {
return this.findOne({ _id: id, clientSecret: clientSecret }) return this.findOne({ _id: id, clientSecret: clientSecret }).exec()
} }

View File

@ -28,7 +28,7 @@ mongoose.model('OAuthToken', OAuthTokenSchema)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
function getByRefreshTokenAndPopulateClient (refreshToken) { function getByRefreshTokenAndPopulateClient (refreshToken) {
return this.findOne({ refreshToken: refreshToken }).populate('client').then(function (token) { return this.findOne({ refreshToken: refreshToken }).populate('client').exec().then(function (token) {
if (!token) return token if (!token) return token
const tokenInfos = { const tokenInfos = {
@ -47,9 +47,9 @@ function getByRefreshTokenAndPopulateClient (refreshToken) {
} }
function getByTokenAndPopulateUser (bearerToken) { function getByTokenAndPopulateUser (bearerToken) {
return this.findOne({ accessToken: bearerToken }).populate('user') return this.findOne({ accessToken: bearerToken }).populate('user').exec()
} }
function getByRefreshToken (refreshToken) { function getByRefreshToken (refreshToken) {
return this.findOne({ refreshToken: refreshToken }) return this.findOne({ refreshToken: refreshToken }).exec()
} }