mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-19 13:56:04 +00:00
exposed bknd middlewares to be used for custom routes
This commit is contained in:
@@ -111,7 +111,6 @@ export class App {
|
||||
await Promise.all(this.plugins.map((plugin) => plugin(this)));
|
||||
}
|
||||
|
||||
//console.log("emitting built", options);
|
||||
await this.emgr.emit(new AppBuiltEvent({ app: this }));
|
||||
|
||||
server.all("/api/*", async (c) => c.notFound());
|
||||
|
||||
@@ -33,17 +33,17 @@ export const auth = (options?: {
|
||||
c.set("auth_registered", true);
|
||||
|
||||
const app = c.get("app");
|
||||
const skipped = shouldSkip(c, options?.skip) || !app.module.auth.enabled;
|
||||
const guard = app.modules.ctx().guard;
|
||||
const authenticator = app.module.auth.authenticator;
|
||||
const skipped = shouldSkip(c, options?.skip) || !app?.module.auth.enabled;
|
||||
const guard = app?.modules.ctx().guard;
|
||||
const authenticator = app?.module.auth.authenticator;
|
||||
|
||||
if (!skipped) {
|
||||
const resolved = c.get("auth_resolved");
|
||||
if (!resolved) {
|
||||
if (!app.module.auth.enabled) {
|
||||
guard.setUserContext(undefined);
|
||||
guard?.setUserContext(undefined);
|
||||
} else {
|
||||
guard.setUserContext(await authenticator.resolveAuthFromRequest(c));
|
||||
guard?.setUserContext(await authenticator?.resolveAuthFromRequest(c));
|
||||
c.set("auth_resolved", true);
|
||||
}
|
||||
}
|
||||
@@ -53,11 +53,11 @@ export const auth = (options?: {
|
||||
|
||||
if (!skipped) {
|
||||
// renew cookie if applicable
|
||||
authenticator.requestCookieRefresh(c);
|
||||
authenticator?.requestCookieRefresh(c);
|
||||
}
|
||||
|
||||
// release
|
||||
guard.setUserContext(undefined);
|
||||
guard?.setUserContext(undefined);
|
||||
authenticator?.resetUser();
|
||||
c.set("auth_resolved", false);
|
||||
});
|
||||
@@ -75,7 +75,7 @@ export const permission = (
|
||||
//console.log("skip?", c.get("auth_skip"));
|
||||
|
||||
// in tests, app is not defined
|
||||
if (!c.get("auth_registered")) {
|
||||
if (!c.get("auth_registered") || !app) {
|
||||
const msg = `auth middleware not registered, cannot check permissions for ${getPath(c)}`;
|
||||
if (app?.module.auth.enabled) {
|
||||
throw new Error(msg);
|
||||
|
||||
@@ -9,6 +9,7 @@ export {
|
||||
type ModuleBuildContext
|
||||
} from "./modules/ModuleManager";
|
||||
|
||||
export * as middlewares from "modules/middlewares";
|
||||
export { registries } from "modules/registries";
|
||||
|
||||
export type * from "./adapter";
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import { auth, permission } from "auth/middlewares";
|
||||
import { Hono } from "hono";
|
||||
import type { ServerEnv } from "modules/Module";
|
||||
|
||||
const middlewares = {
|
||||
auth,
|
||||
permission
|
||||
} as const;
|
||||
import * as middlewares from "modules/middlewares";
|
||||
|
||||
export class Controller {
|
||||
protected middlewares = middlewares;
|
||||
|
||||
@@ -9,13 +9,13 @@ import type { Hono } from "hono";
|
||||
|
||||
export type ServerEnv = {
|
||||
Variables: {
|
||||
app: App;
|
||||
app?: App;
|
||||
// to prevent resolving auth multiple times
|
||||
auth_resolved: boolean;
|
||||
auth_resolved?: boolean;
|
||||
// to only register once
|
||||
auth_registered: boolean;
|
||||
auth_registered?: boolean;
|
||||
// whether or not to bypass auth
|
||||
auth_skip: boolean;
|
||||
auth_skip?: boolean;
|
||||
html?: string;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -11,7 +11,7 @@ export {
|
||||
MODULE_NAMES,
|
||||
type ModuleKey
|
||||
} from "./ModuleManager";
|
||||
export { /*Module,*/ type ModuleBuildContext } from "./Module";
|
||||
export type { ModuleBuildContext } from "./Module";
|
||||
|
||||
export {
|
||||
type PrimaryFieldType,
|
||||
|
||||
1
app/src/modules/middlewares.ts
Normal file
1
app/src/modules/middlewares.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { auth, permission } from "auth/middlewares";
|
||||
@@ -292,7 +292,7 @@ export class SystemController extends Controller {
|
||||
return c.json({
|
||||
version: this.app.version(),
|
||||
test: 2,
|
||||
app: c.get("app").version()
|
||||
app: c.get("app")?.version()
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user