import fs from "node:fs"; import path from "node:path"; import { DatabaseSync } from "node:sqlite"; import { apiKey } from "@better-auth/api-key"; import { betterAuth } from "better-auth"; import { jwt, testUtils } from "better-auth/plugins"; import { Pool } from "pg"; export function getDatabaseConnection() { const dbUrl = process.env.BETTER_AUTH_DATABASE_PATH ?? "./.data/auth.sqlite"; if (dbUrl.startsWith("postgres://") || dbUrl.startsWith("postgresql://")) { return new Pool({ connectionString: dbUrl, }); } const sqlitePath = dbUrl.replace(/^(sqlite:|file:)/, ""); const dir = path.dirname(sqlitePath); if (dir && dir !== ".") { fs.mkdirSync(dir, { recursive: true }); } return new DatabaseSync(sqlitePath); } export const auth = betterAuth({ database: getDatabaseConnection(), session: { expiresIn: 3600 * 24 * 30, // 30 days updateAge: 3600 * 24, // 1 day }, socialProviders: { google: { clientId: process.env.NUXT_GOOGLE_CLIENT_ID ?? "noClientId", clientSecret: process.env.NUXT_GOOGLE_CLIENT_SECRET ?? "noClientSecret", prompt: "select_account", }, }, plugins: [ apiKey({ rateLimit: { enabled: false, }, }), testUtils(), jwt({ jwt: { audience: process.env.NUXT_PUBLIC_API_BASE ?? "http://localhost:8080", expirationTime: "7d", }, }), ], });