Add Better Auth admin authentication
This commit is contained in:
59
convex/betterAuth/auth.ts
Normal file
59
convex/betterAuth/auth.ts
Normal 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));
|
||||
};
|
||||
Reference in New Issue
Block a user