Windshaft-cartodb/lib/api/middlewares/user.js

30 lines
702 B
JavaScript
Raw Permalink Normal View History

'use strict';
2019-10-07 15:58:21 +08:00
const CdbRequest = require('../../models/cdb-request');
2020-02-20 18:47:44 +08:00
module.exports = function user (metadataBackend) {
const cdbRequest = new CdbRequest();
return function userMiddleware (req, res, next) {
try {
res.locals.user = getUserNameFromRequest(req, cdbRequest);
} catch (err) {
return next(err);
}
2020-04-27 16:23:11 +08:00
metadataBackend.getUserId(res.locals.user, (err, userId) => {
if (err || !userId) {
return next();
2020-02-20 18:47:44 +08:00
}
2020-04-27 16:23:11 +08:00
res.locals.userId = userId;
2020-04-27 17:06:09 +08:00
return next();
2020-02-20 18:47:44 +08:00
});
};
};
2020-02-20 18:47:44 +08:00
function getUserNameFromRequest (req, cdbRequest) {
return cdbRequest.userByReq(req);
}