@httpc/kit
Version:
httpc toolbox for building function-based API with minimal code and end-to-end type safety
27 lines (26 loc) • 780 B
JavaScript
import { UnauthorizedError, useContext } from "@httpc/server";
import { useAuthorize } from "./context";
export function Authenticated(permissions) {
return (call, next) => {
const { user } = useContext();
if (!user) {
throw new UnauthorizedError();
}
if (permissions) {
checkAuthorization(call, permissions);
}
return next(call);
};
}
export function Authorized(permissions) {
return (call, next) => {
checkAuthorization(call, permissions);
return next(call);
};
}
function checkAuthorization(call, permissions) {
if (typeof permissions === "function") {
permissions = permissions(...call.params);
}
useAuthorize(permissions);
}