typescript-rest-jwt
Version:
JWT authentication for typescript-rest via decorators.
54 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var expressJWT = require("express-jwt");
var AuthHandler = /** @class */ (function () {
function AuthHandler() {
}
AuthHandler.addPath = function (path, target, propertyKey) {
var className = target.name || target.constructor.name;
var cache = AuthHandler.cache.get(className) || {};
if (!propertyKey) {
var props = Object.getOwnPropertyNames(target.prototype);
props.forEach(function (p) {
if (p !== 'constructor' && p in cache) {
cache[p] = ("" + path + cache[p]).replace(/\/+$/, '');
}
});
return;
}
cache[propertyKey] = path;
AuthHandler.cache.set(className, cache);
};
AuthHandler.configure = function (app, options) {
var ops = typeof options === 'string'
? AuthHandler.createDefaultJWTOptions(options)
: options;
var unique = new Set();
AuthHandler.cache.forEach(function (cache) {
// tslint:disable-next-line forin
for (var route in cache) {
unique.add(cache[route]);
}
});
unique.forEach(function (route) {
app.use(route, expressJWT(ops));
});
};
AuthHandler.cache = new Map();
AuthHandler.createDefaultJWTOptions = function (secret) { return ({
secret: secret,
credentialsRequired: true,
getToken: function (req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
}
else if (req.query && req.query.token) {
return req.query.token;
}
return undefined;
}
}); };
return AuthHandler;
}());
exports.AuthHandler = AuthHandler;
//# sourceMappingURL=AuthHandler.js.map