Add Better Auth admin authentication

This commit is contained in:
2026-06-04 12:05:07 +02:00
parent 0f10bd6400
commit e660ec24aa
41 changed files with 2225 additions and 284 deletions

59
convex/betterAuth/auth.ts Normal file
View File

@@ -0,0 +1,59 @@
import { createClient } from "@convex-dev/better-auth";
import { convex } from "@convex-dev/better-auth/plugins";
import type { GenericCtx } from "@convex-dev/better-auth";
import { betterAuth, type BetterAuthOptions } from "better-auth/minimal";
import type { FunctionReference } from "convex/server";
import { components } from "../_generated/api";
import type { DataModel } from "../_generated/dataModel";
import authConfig from "../auth.config";
import { getBetterAuthServerConfig } from "./env";
import schema from "./schema";
type BetterAuthComponentApi = {
adapter: {
create: FunctionReference<"mutation", "internal">;
findOne: FunctionReference<"query", "internal">;
findMany: FunctionReference<"query", "internal">;
updateOne: FunctionReference<"mutation", "internal">;
updateMany: FunctionReference<"mutation", "internal">;
deleteOne: FunctionReference<"mutation", "internal">;
deleteMany: FunctionReference<"mutation", "internal">;
};
};
const componentsWithAuth = components as {
betterAuth: BetterAuthComponentApi;
};
export const authComponent = createClient<DataModel, typeof schema>(
componentsWithAuth.betterAuth,
{
local: { schema },
verbose: false,
},
);
export const createAuthOptions = (
ctx: GenericCtx<DataModel>,
options: { allowMissingSecret?: boolean } = {},
) => {
const { appUrl, secret } = getBetterAuthServerConfig(process.env, options);
return {
appName: "WebDev Pipeline",
baseURL: appUrl,
secret,
database: authComponent.adapter(ctx),
emailAndPassword: {
enabled: true,
disableSignUp: true,
requireEmailVerification: false,
},
plugins: [convex({ authConfig })],
} satisfies BetterAuthOptions;
};
export const createAuth = (ctx: GenericCtx<DataModel>) => {
return betterAuth(createAuthOptions(ctx));
};