fix double registration of auth middleware on data routes

This commit is contained in:
dswbx
2025-01-16 15:45:29 +01:00
parent 26a5fd8b34
commit 8226b644ae
2 changed files with 19 additions and 18 deletions

View File

@@ -26,21 +26,23 @@ export const auth = (options?: {
skip?: (string | RegExp)[]; skip?: (string | RegExp)[];
}) => }) =>
createMiddleware<ServerEnv>(async (c, next) => { createMiddleware<ServerEnv>(async (c, next) => {
// make sure to only register once
if (c.get("auth_registered")) {
throw new Error(`auth middleware already registered for ${getPath(c)}`);
}
c.set("auth_registered", true);
const app = c.get("app"); const app = c.get("app");
const skipped = shouldSkip(c, options?.skip) || !app?.module.auth.enabled;
const guard = app?.modules.ctx().guard; const guard = app?.modules.ctx().guard;
const authenticator = app?.module.auth.authenticator; const authenticator = app?.module.auth.authenticator;
let skipped = shouldSkip(c, options?.skip) || !app?.module.auth.enabled;
// make sure to only register once
if (c.get("auth_registered")) {
skipped = true;
console.warn(`auth middleware already registered for ${getPath(c)}`);
} else {
c.set("auth_registered", true);
if (!skipped) { if (!skipped) {
const resolved = c.get("auth_resolved"); const resolved = c.get("auth_resolved");
if (!resolved) { if (!resolved) {
if (!app.module.auth.enabled) { if (!app?.module.auth.enabled) {
guard?.setUserContext(undefined); guard?.setUserContext(undefined);
} else { } else {
guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c)); guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c));
@@ -48,6 +50,7 @@ export const auth = (options?: {
} }
} }
} }
}
await next(); await next();

View File

@@ -70,7 +70,7 @@ export class DataController extends Controller {
override getController() { override getController() {
const { permission, auth } = this.middlewares; const { permission, auth } = this.middlewares;
const hono = this.create().use(auth()); const hono = this.create().use(auth(), permission(SystemPermissions.accessApi));
const definedEntities = this.em.entities.map((e) => e.name); const definedEntities = this.em.entities.map((e) => e.name);
const tbNumber = Type.Transform(Type.String({ pattern: "^[1-9][0-9]{0,}$" })) const tbNumber = Type.Transform(Type.String({ pattern: "^[1-9][0-9]{0,}$" }))
@@ -85,8 +85,6 @@ export class DataController extends Controller {
return func; return func;
} }
hono.use("*", permission(SystemPermissions.accessApi));
// info // info
hono.get( hono.get(
"/", "/",