Refactor asset handling and authentication logic (for node)

Updated asset path configuration and server-side logic to standardize asset serving. Introduced `shouldSkipAuth` to bypass authentication for asset requests. Added test coverage for the new asset path handling logic.
This commit is contained in:
dswbx
2025-01-10 20:58:03 +01:00
parent 87e07570d4
commit 3bf92a8c65
7 changed files with 65 additions and 27 deletions

View File

@@ -1,7 +1,7 @@
/** @jsxImportSource hono/jsx */
import type { App } from "App";
import { isDebug } from "core";
import { config, isDebug } from "core";
import { addFlashMessage } from "core/server/flash";
import { html } from "hono/html";
import { Fragment } from "hono/jsx";
@@ -13,6 +13,7 @@ const htmlBkndContextReplace = "<!-- BKND_CONTEXT -->";
// @todo: add migration to remove admin path from config
export type AdminControllerOptions = {
basepath?: string;
assets_path?: string;
html?: string;
forceDev?: boolean | { mainPath: string };
};
@@ -20,7 +21,7 @@ export type AdminControllerOptions = {
export class AdminController extends Controller {
constructor(
private readonly app: App,
private options: AdminControllerOptions = {}
private _options: AdminControllerOptions = {}
) {
super();
}
@@ -29,6 +30,14 @@ export class AdminController extends Controller {
return this.app.modules.ctx();
}
get options() {
return {
...this._options,
basepath: this._options.basepath ?? "/",
assets_path: this._options.assets_path ?? config.server.assets_path
};
}
get basepath() {
return this.options.basepath ?? "/";
}
@@ -156,8 +165,16 @@ export class AdminController extends Controller {
<title>BKND</title>
{isProd ? (
<Fragment>
<script type="module" CrossOrigin src={"/" + assets?.js} />
<link rel="stylesheet" crossOrigin href={"/" + assets?.css} />
<script
type="module"
CrossOrigin
src={this.options.assets_path + assets?.js}
/>
<link
rel="stylesheet"
crossOrigin
href={this.options.assets_path + assets?.css}
/>
</Fragment>
) : (
<Fragment>