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;
};
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;

View file

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

View file

@ -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;
});

View file

@ -1,4 +1,4 @@
import "./setup.ts";
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 * 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();
});