fix: Centralise user deserialization

This commit is contained in:
Bouncey
2019-03-04 21:10:12 +00:00
committed by mrugesh mohapatra
parent 0c23844793
commit 72a0d63aa0
6 changed files with 132 additions and 95 deletions

View File

@ -1,5 +1,6 @@
import loopback from 'loopback';
import { isEmpty } from 'lodash';
import { getUserById as _getUserById } from '../utils/user-stats';
import {
getAccessTokenFromRequest,
errorTypes,
@ -63,7 +64,6 @@ export default ({ jwtSecret = _jwtSecret, getUserById = _getUserById } = {}) =>
return getUserById(userId)
.then(user => {
if (user) {
user.points = user.progressTimestamps.length;
req.user = user;
}
return;
@ -76,15 +76,3 @@ export default ({ jwtSecret = _jwtSecret, getUserById = _getUserById } = {}) =>
}
return Promise.resolve(next());
};
export function _getUserById(id) {
const User = loopback.getModelByType('User');
return new Promise((resolve, reject) =>
User.findById(id, (err, instance) => {
if (err) {
return reject(err);
}
return resolve(instance);
})
);
}