50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { betterAuth } from "better-auth";
|
|
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
|
import { dbSqlite, dbPostgres } from "./database/";
|
|
import * as authSchema from "./database/schema/better-auth";
|
|
import { username, admin, apiKey, openAPI } from "better-auth/plugins";
|
|
import { tanstackStartCookies } from "better-auth/tanstack-start";
|
|
import * as secStorage from "./better-auth.session";
|
|
import dc from "./drizzle.config";
|
|
|
|
export const auth = betterAuth({
|
|
emailAndPassword: { enabled: true },
|
|
plugins: [username(), admin({ adminRoles: ["admin"] }), apiKey(), openAPI(), tanstackStartCookies()],
|
|
basePath: "/auth",
|
|
database:
|
|
dc.dialect === "postgresql"
|
|
? drizzleAdapter(dbPostgres(), {
|
|
provider: "pg",
|
|
schema: authSchema,
|
|
})
|
|
: drizzleAdapter(dbSqlite(), {
|
|
provider: "sqlite",
|
|
schema: authSchema,
|
|
}),
|
|
secondaryStorage: process.env.REDIS_URL ? secStorage.storageRedis : secStorage.storageMem,
|
|
});
|
|
|
|
let _schema: ReturnType<typeof auth.api.generateOpenAPISchema>;
|
|
// biome-ignore lint/suspicious/noAssignInExpressions: elysiajs told me
|
|
const getSchema = async () => (_schema ??= auth.api.generateOpenAPISchema());
|
|
|
|
export const bAuthOpenAPI = {
|
|
getPaths: (prefix = "/auth/api") =>
|
|
getSchema().then(({ paths }) => {
|
|
const reference: typeof paths = Object.create(null);
|
|
|
|
for (const path of Object.keys(paths)) {
|
|
const key = prefix + path;
|
|
reference[key] = paths[path];
|
|
|
|
for (const method of Object.keys(paths[path])) {
|
|
const operation = (reference[key] as any)[method];
|
|
|
|
operation.tags = ["Better Auth"];
|
|
}
|
|
}
|
|
|
|
return reference;
|
|
}) as Promise<any>,
|
|
components: getSchema().then(({ components }) => components) as Promise<any>,
|
|
} as const;
|