confirmed SSR support with Remix

This commit is contained in:
dswbx
2024-11-25 19:59:46 +01:00
parent 1c94777317
commit eea76ebc28
15 changed files with 144 additions and 44 deletions

View File

@@ -6,6 +6,8 @@ export type BaseModuleApiOptions = {
host: string;
basepath?: string;
token?: string;
headers?: Headers;
token_transport?: "header" | "cookie" | "none";
};
export type ApiResponse<Data = any> = {
@@ -53,14 +55,18 @@ export abstract class ModuleApi<Options extends BaseModuleApiOptions> {
}
}
const headers = new Headers(_init?.headers ?? {});
const headers = new Headers(this.options.headers ?? {});
// add init headers
for (const [key, value] of Object.entries(_init?.headers ?? {})) {
headers.set(key, value as string);
}
headers.set("Accept", "application/json");
if (this.options.token) {
// only add token if initial headers not provided
if (this.options.token && this.options.token_transport === "header") {
//console.log("setting token", this.options.token);
headers.set("Authorization", `Bearer ${this.options.token}`);
} else {
//console.log("no token");
}
let body: any = _init?.body;

View File

@@ -38,20 +38,26 @@ export class AdminController implements ClassController {
return this.app.modules.ctx();
}
private withBasePath(route: string = "") {
return (this.app.modules.configs().server.admin.basepath + route).replace(/\/+$/, "/");
}
getController(): Hono<any> {
const auth = this.app.module.auth;
const configs = this.app.modules.configs();
// if auth is not enabled, authenticator is undefined
const auth_enabled = configs.auth.enabled;
const basepath = (String(configs.server.admin.basepath) + "/").replace(/\/+$/, "/");
const hono = new Hono<{
Variables: {
html: string;
};
}>().basePath(basepath);
}>().basePath(this.withBasePath());
hono.use("*", async (c, next) => {
const obj = { user: auth.authenticator?.getUser() };
const obj = {
user: auth.authenticator?.getUser(),
logout_route: this.withBasePath(authRoutes.logout)
};
const html = await this.getHtml(obj);
if (!html) {
console.warn("Couldn't generate HTML for admin UI");
@@ -85,7 +91,6 @@ export class AdminController implements ClassController {
}
hono.get("*", async (c) => {
console.log("admin", c.req.url);
if (!this.ctx.guard.granted(SystemPermissions.accessAdmin)) {
await addFlashMessage(c, "You are not authorized to access the Admin UI", "error");
return c.redirect(authRoutes.login);