nuxt-security
Version:
🛡️ Security Module for Nuxt based on HTTP Headers and Middleware
35 lines (34 loc) • 1.38 kB
JavaScript
import { createRouter, toRouteMatcher } from "radix3";
import { defuReplaceArray } from "../../../utils/merge";
const nitroAppSecurityOptions = {};
export function getAppSecurityOptions() {
return nitroAppSecurityOptions;
}
export function resolveSecurityRules(event) {
if (!event.context.security) {
event.context.security = {};
}
if (!event.context.security.rules) {
const router = createRouter({ routes: structuredClone(nitroAppSecurityOptions) });
const matcher = toRouteMatcher(router);
const eventPathNoQuery = event.path.split("?")[0];
const matches = eventPathNoQuery ? matcher.matchAll(eventPathNoQuery) : [];
const rules = defuReplaceArray({}, ...matches.reverse());
event.context.security.rules = rules;
}
return event.context.security.rules;
}
export function resolveSecurityRoute(event) {
if (!event.context.security) {
event.context.security = {};
}
if (!event.context.security.route) {
const routeNames = Object.fromEntries(Object.entries(nitroAppSecurityOptions).map(([name]) => [name, { name }]));
const router = createRouter({ routes: routeNames });
const eventPathNoQuery = event.path.split("?")[0];
const match = eventPathNoQuery ? router.lookup(eventPathNoQuery) : void 0;
const route = match?.name ?? "";
event.context.security.route = route;
}
return event.context.security.route;
}