@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
32 lines (31 loc) • 989 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Authorized = exports.Authenticated = void 0;
const server_1 = require("@httpc/server");
const context_1 = require("./context");
function Authenticated(permissions) {
return (call, next) => {
const { user } = (0, server_1.useContext)();
if (!user) {
throw new server_1.UnauthorizedError();
}
if (permissions) {
checkAuthorization(call, permissions);
}
return next(call);
};
}
exports.Authenticated = Authenticated;
function Authorized(permissions) {
return (call, next) => {
checkAuthorization(call, permissions);
return next(call);
};
}
exports.Authorized = Authorized;
function checkAuthorization(call, permissions) {
if (typeof permissions === "function") {
permissions = permissions(...call.params);
}
(0, context_1.useAuthorize)(permissions);
}