mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 21:06:04 +00:00
fix double registration of auth middleware on data routes
This commit is contained in:
@@ -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();
|
||||||
|
|
||||||
|
|||||||
@@ -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(
|
||||||
"/",
|
"/",
|
||||||
|
|||||||
Reference in New Issue
Block a user