refactor: auth to proxy pattern

This commit is contained in:
Nicolas Meienberger 2026-01-08 20:44:42 +01:00
parent 10b85fdd42
commit a4464d243a
5 changed files with 17 additions and 13 deletions

View file

@ -61,15 +61,16 @@ const createAuth = async (): Promise<Auth> => {
return _auth; return _auth;
}; };
export const auth: Auth = { export const auth = new Proxy(
get api() { {},
if (!_auth) throw new Error("Auth not initialized. Call initAuth() first."); {
return _auth.api; get(_, prop, receiver) {
if (!_auth) {
throw new Error("Auth not initialized. Call initAuth() first.");
}
return Reflect.get(_auth, prop, receiver);
},
}, },
get handler() { ) as Auth;
if (!_auth) throw new Error("Auth not initialized. Call initAuth() first.");
return _auth.handler;
},
} as Auth;
export const initAuth = createAuth; export const initAuth = createAuth;

View file

@ -16,10 +16,10 @@ if (cliRun) {
process.exit(0); process.exit(0);
} }
const app = createApp();
runDbMigrations(); runDbMigrations();
const app = createApp();
await runMigrations(); await runMigrations();
await startup(); await startup();

View file

@ -14,6 +14,7 @@ import { notificationsService } from "../notifications/notifications.service";
import { VolumeAutoRemountJob } from "~/server/jobs/auto-remount"; import { VolumeAutoRemountJob } from "~/server/jobs/auto-remount";
import { cache } from "~/server/utils/cache"; import { cache } from "~/server/utils/cache";
import { initAuth } from "~/lib/auth"; import { initAuth } from "~/lib/auth";
import { toMessage } from "~/server/utils/errors";
const ensureLatestConfigurationSchema = async () => { const ensureLatestConfigurationSchema = async () => {
const volumes = await db.query.volumesTable.findMany({}); const volumes = await db.query.volumesTable.findMany({});
@ -52,7 +53,7 @@ export const startup = async () => {
}); });
await initAuth().catch((err) => { await initAuth().catch((err) => {
logger.error(`Error initializing auth: ${err.message}`); logger.error(`Error initializing auth: ${toMessage(err)}`);
throw err; throw err;
}); });

View file

@ -1,4 +1,4 @@
import "./setup.ts"; import "./setup.ts";
import { GlobalRegistrator } from "@happy-dom/global-registrator"; import { GlobalRegistrator } from "@happy-dom/global-registrator";
GlobalRegistrator.register(); GlobalRegistrator.register({ url: "http://localhost:3000" });

View file

@ -4,6 +4,7 @@ import path from "node:path";
import { cwd } from "node:process"; import { cwd } from "node:process";
import * as schema from "~/server/db/schema"; import * as schema from "~/server/db/schema";
import { db, setSchema } from "~/server/db/db"; import { db, setSchema } from "~/server/db/db";
import { initAuth } from "~/lib/auth";
setSchema(schema); setSchema(schema);
@ -27,4 +28,5 @@ void mock.module("~/server/utils/crypto", () => ({
beforeAll(async () => { beforeAll(async () => {
const migrationsFolder = path.join(cwd(), "app", "drizzle"); const migrationsFolder = path.join(cwd(), "app", "drizzle");
migrate(db, { migrationsFolder }); migrate(db, { migrationsFolder });
await initAuth();
}); });