Store userId in res.locals

This commit is contained in:
Esther Lozano 2020-02-20 11:47:44 +01:00
parent 59cb6f9c9c
commit 9591a5a2b0
2 changed files with 23 additions and 4 deletions

View File

@ -200,6 +200,7 @@ module.exports = class ApiRouter {
this.mapRouter = new MapRouter({ collaborators });
this.templateRouter = new TemplateRouter({ collaborators });
this.metadataBackend = metadataBackend;
}
route (app, routes) {
@ -224,7 +225,7 @@ module.exports = class ApiRouter {
}));
apiRouter.use(lzmaMiddleware());
apiRouter.use(cors());
apiRouter.use(user());
apiRouter.use(user(this.metadataBackend));
this.templateRouter.route(apiRouter, route.template);
this.mapRouter.route(apiRouter, route.map);

View File

@ -2,12 +2,30 @@
const CdbRequest = require('../../models/cdb-request');
module.exports = function user () {
module.exports = function user (metadataBackend) {
const cdbRequest = new CdbRequest();
return function userMiddleware (req, res, next) {
res.locals.user = cdbRequest.userByReq(req);
res.locals.user = getUserNameFromRequest(req, cdbRequest);
next();
getUserId(metadataBackend, res.locals.user, function (userId) {
if (userId) {
res.locals.userId = userId;
}
return next();
});
};
};
function getUserNameFromRequest (req, cdbRequest) {
return cdbRequest.userByReq(req);
}
function getUserId (metadataBackend, userName, callback) {
metadataBackend.getUserId(userName, function (err, userId) {
if (err) {
return callback();
}
return callback(userId);
});
}