Use Drizzle adapter and Drizzle CLI auth migrations
This commit is contained in:
115
lib/auth.ts
115
lib/auth.ts
@@ -1,18 +1,13 @@
|
||||
import { betterAuth } from 'better-auth';
|
||||
import { getMigrations } from 'better-auth/db';
|
||||
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
|
||||
import { nextCookies } from 'better-auth/next-js';
|
||||
import { admin, magicLink, organization } from 'better-auth/plugins';
|
||||
import { Pool } from 'pg';
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __fiscalAuthPgPool: Pool | undefined;
|
||||
}
|
||||
import { db } from '@/lib/server/db';
|
||||
import { authSchema } from '@/lib/server/db/schema';
|
||||
|
||||
type BetterAuthInstance = ReturnType<typeof betterAuth>;
|
||||
|
||||
let authInstance: BetterAuthInstance | null = null;
|
||||
let migrationPromise: Promise<void> | null = null;
|
||||
|
||||
function parseCsvList(value: string | undefined) {
|
||||
return (value ?? '')
|
||||
@@ -21,80 +16,6 @@ function parseCsvList(value: string | undefined) {
|
||||
.filter((entry) => entry.length > 0);
|
||||
}
|
||||
|
||||
function isPostgresConnectionString(value: string | undefined) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const protocol = new URL(value).protocol.toLowerCase();
|
||||
return protocol === 'postgres:' || protocol === 'postgresql:';
|
||||
} catch {
|
||||
return /^postgres(?:ql)?:\/\//i.test(value);
|
||||
}
|
||||
}
|
||||
|
||||
function splitSqlStatements(sqlText: string) {
|
||||
return sqlText
|
||||
.split(';')
|
||||
.map((statement) => statement.trim())
|
||||
.filter((statement) => statement.length > 0);
|
||||
}
|
||||
|
||||
function buildPostgresMigrationPlan(sqlText: string) {
|
||||
const immediateStatements: string[] = [];
|
||||
const deferredIndexStatements: string[] = [];
|
||||
const addIndexPattern = /^alter table\s+([^\s]+)\s+add\s+index\s+([^\s]+)\s+\((.+)\)$/i;
|
||||
|
||||
for (const rawStatement of splitSqlStatements(sqlText)) {
|
||||
const statement = rawStatement.replace(/\s+/g, ' ').trim();
|
||||
const match = statement.match(addIndexPattern);
|
||||
if (!match) {
|
||||
immediateStatements.push(statement);
|
||||
continue;
|
||||
}
|
||||
|
||||
const [, tableName, indexName, columns] = match;
|
||||
deferredIndexStatements.push(`create index if not exists ${indexName} on ${tableName} (${columns})`);
|
||||
}
|
||||
|
||||
return [...immediateStatements, ...deferredIndexStatements];
|
||||
}
|
||||
|
||||
async function runPostgresMigrations(pool: Pool, sqlText: string) {
|
||||
const statements = buildPostgresMigrationPlan(sqlText);
|
||||
if (statements.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
for (const statement of statements) {
|
||||
await client.query(statement);
|
||||
}
|
||||
await client.query('COMMIT');
|
||||
} catch (error) {
|
||||
await client.query('ROLLBACK');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
function getPool() {
|
||||
const connectionString = process.env.DATABASE_URL?.trim();
|
||||
if (!connectionString) {
|
||||
throw new Error('DATABASE_URL is required for Better Auth PostgreSQL adapter.');
|
||||
}
|
||||
|
||||
if (!globalThis.__fiscalAuthPgPool) {
|
||||
globalThis.__fiscalAuthPgPool = new Pool({ connectionString });
|
||||
}
|
||||
|
||||
return globalThis.__fiscalAuthPgPool;
|
||||
}
|
||||
|
||||
function buildAuth() {
|
||||
const adminUserIds = parseCsvList(process.env.BETTER_AUTH_ADMIN_USER_IDS);
|
||||
const trustedOrigins = parseCsvList(process.env.BETTER_AUTH_TRUSTED_ORIGINS);
|
||||
@@ -104,7 +25,10 @@ function buildAuth() {
|
||||
const secret = process.env.BETTER_AUTH_SECRET?.trim() || undefined;
|
||||
|
||||
return betterAuth({
|
||||
database: getPool(),
|
||||
database: drizzleAdapter(db, {
|
||||
provider: 'pg',
|
||||
schema: authSchema
|
||||
}),
|
||||
baseURL,
|
||||
secret,
|
||||
emailAndPassword: {
|
||||
@@ -133,28 +57,5 @@ export function getAuth() {
|
||||
}
|
||||
|
||||
export async function ensureAuthSchema() {
|
||||
const auth = getAuth();
|
||||
const connectionString = process.env.DATABASE_URL?.trim();
|
||||
|
||||
if (!migrationPromise) {
|
||||
migrationPromise = (async () => {
|
||||
const migrations = await getMigrations(auth.options);
|
||||
if (isPostgresConnectionString(connectionString)) {
|
||||
const sql = await migrations.compileMigrations();
|
||||
await runPostgresMigrations(getPool(), sql);
|
||||
return;
|
||||
}
|
||||
|
||||
await migrations.runMigrations();
|
||||
})();
|
||||
}
|
||||
|
||||
try {
|
||||
await migrationPromise;
|
||||
} catch (error) {
|
||||
migrationPromise = null;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return auth;
|
||||
return getAuth();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user