fix: defer auth initialization until after passfile exists

This commit is contained in:
Jakub Trávník 2026-01-08 14:57:06 +01:00
parent 99932a8522
commit 89315bb8a1
2 changed files with 52 additions and 28 deletions

View file

@ -14,7 +14,12 @@ import { ensureOnlyOneUser } from "./auth-middlewares/only-one-user";
export type AuthMiddlewareContext = MiddlewareContext<MiddlewareOptions, AuthContext<BetterAuthOptions>>; export type AuthMiddlewareContext = MiddlewareContext<MiddlewareOptions, AuthContext<BetterAuthOptions>>;
export const auth = betterAuth({ let _auth: ReturnType<typeof betterAuth> | null = null;
const createAuth = async () => {
if (_auth) return _auth;
_auth = betterAuth({
secret: await cryptoUtils.deriveSecret("better-auth"), secret: await cryptoUtils.deriveSecret("better-auth"),
hooks: { hooks: {
before: createAuthMiddleware(async (ctx) => { before: createAuthMiddleware(async (ctx) => {
@ -47,3 +52,19 @@ export const auth = betterAuth({
}, },
plugins: [username({})], plugins: [username({})],
}); });
return _auth;
};
export const auth = {
get api() {
if (!_auth) throw new Error("Auth not initialized. Call initAuth() first.");
return _auth.api;
},
get handler() {
if (!_auth) throw new Error("Auth not initialized. Call initAuth() first.");
return _auth.handler;
},
};
export const initAuth = createAuth;

View file

@ -50,6 +50,9 @@ export const startup = async () => {
logger.error(`Error ensuring restic passfile exists: ${err.message}`); logger.error(`Error ensuring restic passfile exists: ${err.message}`);
}); });
const { initAuth } = await import("~/lib/auth");
await initAuth();
await ensureLatestConfigurationSchema(); await ensureLatestConfigurationSchema();
const volumes = await db.query.volumesTable.findMany({ const volumes = await db.query.volumesTable.findMany({