Windshaft-cartodb/lib/api/middlewares/user.js
2020-06-02 12:31:18 +02:00

27 lines
723 B
JavaScript

'use strict';
const CdbRequest = require('../../models/cdb-request');
module.exports = function user (metadataBackend) {
return function userMiddleware (req, res, next) {
const { logger } = res.locals;
const cdbRequest = new CdbRequest({ logger });
res.locals.user = getUserNameFromRequest(req, cdbRequest);
metadataBackend.getUserId(res.locals.user, (err, userId) => {
if (err || !userId) {
return next();
}
res.locals.userId = userId;
res.set('Carto-User-Id', `${userId}`);
return next();
});
};
};
function getUserNameFromRequest (req, cdbRequest) {
return cdbRequest.userByReq(req);
}