|
| 1 | +import { createClient, type GenericCtx } from "@convex-dev/better-auth"; |
| 2 | +import { convex } from "@convex-dev/better-auth/plugins"; |
| 3 | +import { components } from "./_generated/api"; |
| 4 | +import { DataModel } from "./_generated/dataModel"; |
| 5 | +import { query } from "./_generated/server"; |
| 6 | +import { betterAuth } from "better-auth/minimal"; |
| 7 | +import authConfig from "./auth.config"; |
| 8 | + |
| 9 | +const siteUrl = process.env.SITE_URL!; |
| 10 | + |
| 11 | +// The component client has methods needed for integrating Convex with Better Auth, |
| 12 | +// as well as helper methods for general use. |
| 13 | +export const authComponent = createClient<DataModel>(components.betterAuth); |
| 14 | + |
| 15 | +export const createAuth = (ctx: GenericCtx<DataModel>) => { |
| 16 | + return betterAuth({ |
| 17 | + baseURL: siteUrl, |
| 18 | + database: authComponent.adapter(ctx), |
| 19 | + // Configure simple, non-verified email/password to get started |
| 20 | + emailAndPassword: { |
| 21 | + enabled: true, |
| 22 | + requireEmailVerification: false, |
| 23 | + }, |
| 24 | + plugins: [ |
| 25 | + // The Convex plugin is required for Convex compatibility |
| 26 | + convex({ authConfig }), |
| 27 | + ], |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +// Example function for getting the current user |
| 32 | +// Feel free to edit, omit, etc. |
| 33 | +export const getCurrentUser = query({ |
| 34 | + args: {}, |
| 35 | + handler: async (ctx) => { |
| 36 | + return authComponent.getAuthUser(ctx); |
| 37 | + }, |
| 38 | +}); |
0 commit comments