use q for mongoose promises and switch to promises from callbacks in queries

This commit is contained in:
Matteo Pagliazzi
2015-11-07 16:28:52 +01:00
parent 0834f8eeea
commit f26737ab79
2 changed files with 13 additions and 8 deletions
+11 -8
View File
@@ -4,7 +4,7 @@ import {
} from '../../libs/api-v3/errors';
import {
UserModel as User,
model as User,
} from '../../models/user';
// TODO use i18n
@@ -27,12 +27,12 @@ export function authWithHeaders (req, res, next) {
return next(new NotAuthorized(missingAuthHeaders));
}
// TODO use promises?
User.findOne({
_id: userId,
apiToken,
}, (err, user) => {
if (err) return next(err);
})
.exec()
.then((user) => {
if (!user) return next(new NotAuthorized(userNotFound));
// TODO better handling for this case
@@ -42,7 +42,8 @@ export function authWithHeaders (req, res, next) {
// TODO use either session/cookie or headers, not both
req.session.userId = user._id;
return next();
});
})
.catch(next);
}
// Authenticate a request through a valid session
@@ -54,11 +55,13 @@ export function authWithSession (req, res, next) {
User.findOne({
_id: userId,
}, (err, user) => {
if (err) return next(err);
})
.exec()
.then((user) => {
if (!user) return next(new NotAuthorized(userNotFound));
res.locals.user = user;
return next();
});
})
.catch(next);
}
+2
View File
@@ -39,6 +39,8 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) {
// ------------ MongoDB Configuration ------------
var mongoose = require('mongoose');
// Use Q promises instead of mpromise in mongoose
mongoose.Promise = require('q');
var mongooseOptions = !isProd ? {} : {
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }