fix: defer auth initialization until after passfile exists
This commit is contained in:
parent
99932a8522
commit
89315bb8a1
2 changed files with 52 additions and 28 deletions
|
|
@ -14,7 +14,12 @@ import { ensureOnlyOneUser } from "./auth-middlewares/only-one-user";
|
|||
|
||||
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"),
|
||||
hooks: {
|
||||
before: createAuthMiddleware(async (ctx) => {
|
||||
|
|
@ -46,4 +51,20 @@ export const auth = betterAuth({
|
|||
modelName: "sessionsTable",
|
||||
},
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -50,6 +50,9 @@ export const startup = async () => {
|
|||
logger.error(`Error ensuring restic passfile exists: ${err.message}`);
|
||||
});
|
||||
|
||||
const { initAuth } = await import("~/lib/auth");
|
||||
await initAuth();
|
||||
|
||||
await ensureLatestConfigurationSchema();
|
||||
|
||||
const volumes = await db.query.volumesTable.findMany({
|
||||
|
|
|
|||
Loading…
Reference in a new issue