diff --git a/convex/auth.ts b/convex/auth.ts index 7c20d754..25e07660 100644 --- a/convex/auth.ts +++ b/convex/auth.ts @@ -1,6 +1,10 @@ -import { createClient, type GenericCtx } from "@convex-dev/better-auth"; +import { + AuthFunctions, + createClient, + type GenericCtx, +} from "@convex-dev/better-auth"; import { convex, crossDomain } from "@convex-dev/better-auth/plugins"; -import { components } from "./_generated/api"; +import { components, internal } from "./_generated/api"; import { DataModel } from "./_generated/dataModel"; import { query } from "./_generated/server"; import { betterAuth } from "better-auth"; @@ -10,6 +14,8 @@ import authSchema from "./betterAuth/schema"; const siteUrl = process.env.PUBLIC_CONVEX_SITE_URL!; // redirects to the convex deployment, which redirects to the referer. if it works don't touch it +const authFunctions: AuthFunctions = internal.auth; + // The component client has methods needed for integrating Convex with Better Auth, // as well as helper methods for general use. export const authComponent = createClient( @@ -18,6 +24,24 @@ export const authComponent = createClient( local: { schema: authSchema, }, + authFunctions, + triggers: { + user: { + onDelete: async (ctx, doc) => { + if (!doc?.userId) { + return; + } + + const backups = await ctx.db + .query("backups") + .withIndex("by_user", (q) => q.eq("userId", doc.userId as string)) + .collect(); + for (const backup of backups) { + await ctx.db.delete(backup._id); + } + }, + }, + }, }, ); @@ -111,3 +135,5 @@ export const getCurrentUser = query({ return authComponent.safeGetAuthUser(ctx); }, }); + +export const { onCreate, onDelete, onUpdate } = authComponent.triggersApi();