import { betterAuth } from "better-auth"; import type { AppConfig } from "../../presentation/config/AppConfig"; import { ApplicationRequestError } from "../../application/ApplicationRequestError"; import type { PrismaDatabaseClient } from "../persistence/PrismaDatabaseClient"; import { CodemationBetterAuthServerFactory } from "./CodemationBetterAuthServerFactory"; export class CodemationBetterAuthRuntime { private cached: ReturnType | undefined; constructor( private readonly appConfig: AppConfig, private readonly serverFactory: CodemationBetterAuthServerFactory, private readonly prisma: PrismaDatabaseClient | undefined, ) {} tryGetAuth(): ReturnType | undefined { if (!this.prisma) { return undefined; } if (!this.cached) { this.cached = this.serverFactory.create(this.prisma); } return this.cached; } getAuthOrThrow(): ReturnType { const auth = this.tryGetAuth(); if (!auth) { throw new ApplicationRequestError(503, "Authentication requires prepared runtime database persistence."); } return auth; } }