2014-07-31 11:27:14 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Module dependencies.
|
|
|
|
|
*/
|
|
|
|
|
var _ = require('lodash'),
|
|
|
|
|
mongoose = require('mongoose'),
|
|
|
|
|
User = mongoose.model('User');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User middleware
|
|
|
|
|
*/
|
2015-07-26 01:40:16 -04:00
|
|
|
exports.userByID = function (req, res, next, id) {
|
|
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
|
return res.status(400).send({
|
|
|
|
|
message: 'User is invalid'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-31 11:27:14 +03:00
|
|
|
User.findOne({
|
|
|
|
|
_id: id
|
2015-07-26 01:40:16 -04:00
|
|
|
}).exec(function (err, user) {
|
2014-07-31 11:27:14 +03:00
|
|
|
if (err) return next(err);
|
|
|
|
|
if (!user) return next(new Error('Failed to load User ' + id));
|
|
|
|
|
req.profile = user;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
};
|