}
function connect () {
+ mongoose.Promise = global.Promise
mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
mongoose.connection.on('error', function () {
throw new Error('Mongodb connection error.')
}
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({
accessToken: token.accessToken,
user: user.id
})
- return tokenObj.save(function (err, tokenCreated) {
- if (err) throw err // node-oauth2-server library uses Promise.try
-
+ return tokenObj.save().then(function (tokenCreated) {
tokenCreated.client = client
tokenCreated.user = user
-
return tokenCreated
+ }).catch(function (err) {
+ throw err
})
}
}
function getByIdAndSecret (id, clientSecret) {
- return this.findOne({ _id: id, clientSecret: clientSecret })
+ return this.findOne({ _id: id, clientSecret: clientSecret }).exec()
}
// ---------------------------------------------------------------------------
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
const tokenInfos = {
}
function getByTokenAndPopulateUser (bearerToken) {
- return this.findOne({ accessToken: bearerToken }).populate('user')
+ return this.findOne({ accessToken: bearerToken }).populate('user').exec()
}
function getByRefreshToken (refreshToken) {
- return this.findOne({ refreshToken: refreshToken })
+ return this.findOne({ refreshToken: refreshToken }).exec()
}