BetterFrame/server/src/plugins/service-admin-http/middleware.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-05-09 23:09:13 +00:00
/**
* Auth & setup gate middleware for admin-http.
*/
import { type H3, getCookie, getRequestPath } from "h3";
2026-05-09 23:09:13 +00:00
import type { AdminDeps } from "./index.js";
import type { User, Session } from "../../shared/types.js";
declare module "h3" {
interface H3EventContext {
user?: User;
session?: Session;
}
}
export function registerMiddleware(app: H3, deps: AdminDeps): void {
app.use((event) => {
const path = getRequestPath(event);
if (
path === "/setup" ||
path.startsWith("/static/") ||
path === "/healthz" ||
path === "/readyz" ||
path === "/version" ||
path === "/api/admin/_check" ||
2026-05-09 23:09:13 +00:00
path === "/"
) {
return;
}
if (!deps.repo.isSetupComplete()) {
2026-05-09 23:09:13 +00:00
if (!path.startsWith("/auth/")) {
return new Response(null, { status: 302, headers: { location: "/setup" } });
}
}
if (path.startsWith("/auth/")) {
return;
}
if (path.startsWith("/admin") || path.startsWith("/api/admin")) {
const cookie = getCookie(event, deps.cookieName);
if (!cookie) {
return new Response(null, { status: 302, headers: { location: "/auth/login" } });
}
const resolved = deps.auth.resolveSession(cookie);
2026-05-09 23:09:13 +00:00
if (!resolved) {
return new Response(null, { status: 302, headers: { location: "/auth/login" } });
}
if (resolved.session.totp_pending) {
return new Response(null, { status: 302, headers: { location: "/auth/totp" } });
}
event.context.user = resolved.user;
event.context.session = resolved.session;
return;
}
});
}