mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
Refactor module middleware initialization logic.
Replaced `getMiddleware` with `onServerInit` for streamlined middleware registration. Updated `AppAuth` to automatically register its authentication middleware. Added a test case to verify middleware registration. Removed redundant cookie renewal logic from `AdminController` and made related adjustments across modules.
This commit is contained in:
@@ -6,14 +6,10 @@ export class Controller {
|
||||
protected middlewares = {
|
||||
auth,
|
||||
permission
|
||||
}
|
||||
};
|
||||
|
||||
protected create({ auth }: { auth?: boolean } = {}): Hono<ServerEnv> {
|
||||
const server = Controller.createServer();
|
||||
if (auth !== false) {
|
||||
server.use(this.middlewares.auth);
|
||||
}
|
||||
return server;
|
||||
protected create(): Hono<ServerEnv> {
|
||||
return Controller.createServer();
|
||||
}
|
||||
|
||||
static createServer(): Hono<ServerEnv> {
|
||||
@@ -23,4 +19,4 @@ export class Controller {
|
||||
getController(): Hono<ServerEnv> {
|
||||
return this.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ export type ServerEnv = {
|
||||
Variables: {
|
||||
app: App;
|
||||
auth_resolved: boolean;
|
||||
auth_registered: boolean;
|
||||
html?: string;
|
||||
};
|
||||
};
|
||||
@@ -87,9 +88,9 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
|
||||
return this._schema;
|
||||
}
|
||||
|
||||
getMiddleware() {
|
||||
return undefined;
|
||||
}
|
||||
// action performed when server has been initialized
|
||||
// can be used to assign global middlewares
|
||||
onServerInit(hono: Hono<ServerEnv>) {}
|
||||
|
||||
get ctx() {
|
||||
if (!this._ctx) {
|
||||
|
||||
@@ -212,14 +212,9 @@ export class ModuleManager {
|
||||
this.options.onServerInit(this.server);
|
||||
}
|
||||
|
||||
// @todo: this is a current workaround, controllers must be reworked
|
||||
// optional method for each module to register global middlewares, etc.
|
||||
objectEach(this.modules, (module) => {
|
||||
if ("getMiddleware" in module) {
|
||||
const middleware = module.getMiddleware();
|
||||
if (middleware) {
|
||||
this.server.use(middleware);
|
||||
}
|
||||
}
|
||||
module.onServerInit(this.server);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -550,4 +545,4 @@ export function getDefaultConfig(): ModuleConfigs {
|
||||
});
|
||||
|
||||
return config as any;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,6 @@ export class AdminController extends Controller {
|
||||
}
|
||||
c.set("html", html);
|
||||
|
||||
// refresh cookie if needed
|
||||
await auth.authenticator?.requestCookieRefresh(c);
|
||||
await next();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user