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

@ -111,7 +111,7 @@ describe('request-authorization', () => {
});
it('adds the user to the request object', async done => {
expect.assertions(5);
expect.assertions(3);
const validJWT = jwt.sign({ accessToken }, validJWTSecret);
const req = mockReq({
path: '/internal/some-path/that-needs/auth',
@ -124,8 +124,6 @@ describe('request-authorization', () => {
expect(next.called).toBe(true);
expect(req).toHaveProperty('user');
expect(req.user).toEqual(users['456def']);
expect(req.user).toHaveProperty('points');
expect(req.user.points).toEqual(4);
return done();
});
@ -200,7 +198,7 @@ describe('request-authorization', () => {
});
it('adds the user to the request object', async done => {
expect.assertions(5);
expect.assertions(3);
const validJWT = jwt.sign({ accessToken }, validJWTSecret);
const req = mockReq({
path: '/internal/some-path/that-needs/auth',
@ -212,8 +210,6 @@ describe('request-authorization', () => {
expect(next.called).toBe(true);
expect(req).toHaveProperty('user');
expect(req.user).toEqual(users['456def']);
expect(req.user).toHaveProperty('points');
expect(req.user.points).toEqual(4);
return done();
});

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);
})
);
}