From a4464d243afd4349b0ff19fd5c7eacaf12303cee Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Thu, 8 Jan 2026 20:44:42 +0100 Subject: [PATCH] refactor: auth to proxy pattern --- app/lib/auth.ts | 19 ++++++++++--------- app/server/index.ts | 4 ++-- app/server/modules/lifecycle/startup.ts | 3 ++- app/test/setup-client.ts | 2 +- app/test/setup.ts | 2 ++ 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/lib/auth.ts b/app/lib/auth.ts index 8b0264a3..f35ac450 100644 --- a/app/lib/auth.ts +++ b/app/lib/auth.ts @@ -61,15 +61,16 @@ const createAuth = async (): Promise => { return _auth; }; -export const auth: Auth = { - get api() { - if (!_auth) throw new Error("Auth not initialized. Call initAuth() first."); - return _auth.api; +export const auth = new Proxy( + {}, + { + get(_, prop, receiver) { + if (!_auth) { + throw new Error("Auth not initialized. Call initAuth() first."); + } + return Reflect.get(_auth, prop, receiver); + }, }, - get handler() { - if (!_auth) throw new Error("Auth not initialized. Call initAuth() first."); - return _auth.handler; - }, -} as Auth; +) as Auth; export const initAuth = createAuth; diff --git a/app/server/index.ts b/app/server/index.ts index 28bd6df4..8faf6a90 100644 --- a/app/server/index.ts +++ b/app/server/index.ts @@ -16,10 +16,10 @@ if (cliRun) { process.exit(0); } -const app = createApp(); - runDbMigrations(); +const app = createApp(); + await runMigrations(); await startup(); diff --git a/app/server/modules/lifecycle/startup.ts b/app/server/modules/lifecycle/startup.ts index 30619cdb..f7e5fadb 100644 --- a/app/server/modules/lifecycle/startup.ts +++ b/app/server/modules/lifecycle/startup.ts @@ -14,6 +14,7 @@ import { notificationsService } from "../notifications/notifications.service"; import { VolumeAutoRemountJob } from "~/server/jobs/auto-remount"; import { cache } from "~/server/utils/cache"; import { initAuth } from "~/lib/auth"; +import { toMessage } from "~/server/utils/errors"; const ensureLatestConfigurationSchema = async () => { const volumes = await db.query.volumesTable.findMany({}); @@ -52,7 +53,7 @@ export const startup = async () => { }); await initAuth().catch((err) => { - logger.error(`Error initializing auth: ${err.message}`); + logger.error(`Error initializing auth: ${toMessage(err)}`); throw err; }); diff --git a/app/test/setup-client.ts b/app/test/setup-client.ts index f737ee3e..a0faa585 100644 --- a/app/test/setup-client.ts +++ b/app/test/setup-client.ts @@ -1,4 +1,4 @@ import "./setup.ts"; import { GlobalRegistrator } from "@happy-dom/global-registrator"; -GlobalRegistrator.register(); +GlobalRegistrator.register({ url: "http://localhost:3000" }); diff --git a/app/test/setup.ts b/app/test/setup.ts index 81927747..ea73e5b0 100644 --- a/app/test/setup.ts +++ b/app/test/setup.ts @@ -4,6 +4,7 @@ import path from "node:path"; import { cwd } from "node:process"; import * as schema from "~/server/db/schema"; import { db, setSchema } from "~/server/db/db"; +import { initAuth } from "~/lib/auth"; setSchema(schema); @@ -27,4 +28,5 @@ void mock.module("~/server/utils/crypto", () => ({ beforeAll(async () => { const migrationsFolder = path.join(cwd(), "app", "drizzle"); migrate(db, { migrationsFolder }); + await initAuth(); });