From 469feb0a7683542d09359bac37fe89a2ea7f9340 Mon Sep 17 00:00:00 2001 From: Ingo Wolf Date: Wed, 10 Dec 2025 15:13:43 +1100 Subject: [PATCH 1/3] add trigger for user deleted --- convex/auth.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/convex/auth.ts b/convex/auth.ts index 7c20d754..5772076b 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,12 +24,20 @@ export const authComponent = createClient( local: { schema: authSchema, }, - }, + authFunctions, + triggers: { + user: { + onDelete: async (ctx, event) => { + console.log("user deleted", event); + }, + }, + }, + } ); export const createAuth = ( ctx: GenericCtx, - { optionsOnly } = { optionsOnly: false }, + { optionsOnly } = { optionsOnly: false } ) => { return betterAuth({ logger: { @@ -111,3 +125,5 @@ export const getCurrentUser = query({ return authComponent.safeGetAuthUser(ctx); }, }); + +export const { onCreate, onDelete, onUpdate } = authComponent.triggersApi(); From 7234700e4407f81b8961a297ea0bedf9f87ba984 Mon Sep 17 00:00:00 2001 From: Ingo Wolf Date: Wed, 10 Dec 2025 15:14:59 +1100 Subject: [PATCH 2/3] add backup deletion --- convex/auth.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/convex/auth.ts b/convex/auth.ts index 5772076b..0b1fe021 100644 --- a/convex/auth.ts +++ b/convex/auth.ts @@ -27,8 +27,18 @@ export const authComponent = createClient( authFunctions, triggers: { user: { - onDelete: async (ctx, event) => { - console.log("user deleted", event); + 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); + } }, }, }, From 42a5d741d8b47c1d606fe529b6f2338fa768d853 Mon Sep 17 00:00:00 2001 From: Inglan Date: Wed, 10 Dec 2025 04:15:20 +0000 Subject: [PATCH 3/3] Prettified Code! --- convex/auth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/convex/auth.ts b/convex/auth.ts index 0b1fe021..25e07660 100644 --- a/convex/auth.ts +++ b/convex/auth.ts @@ -42,12 +42,12 @@ export const authComponent = createClient( }, }, }, - } + }, ); export const createAuth = ( ctx: GenericCtx, - { optionsOnly } = { optionsOnly: false } + { optionsOnly } = { optionsOnly: false }, ) => { return betterAuth({ logger: {