Skip to content

Commit e1eb116

Browse files
authored
Merge pull request #576 from EducationalTools/main
add backup deletion on user delete
2 parents 0e057ea + 42a5d74 commit e1eb116

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

convex/auth.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { createClient, type GenericCtx } from "@convex-dev/better-auth";
1+
import {
2+
AuthFunctions,
3+
createClient,
4+
type GenericCtx,
5+
} from "@convex-dev/better-auth";
26
import { convex, crossDomain } from "@convex-dev/better-auth/plugins";
3-
import { components } from "./_generated/api";
7+
import { components, internal } from "./_generated/api";
48
import { DataModel } from "./_generated/dataModel";
59
import { query } from "./_generated/server";
610
import { betterAuth } from "better-auth";
@@ -10,6 +14,8 @@ import authSchema from "./betterAuth/schema";
1014

1115
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
1216

17+
const authFunctions: AuthFunctions = internal.auth;
18+
1319
// The component client has methods needed for integrating Convex with Better Auth,
1420
// as well as helper methods for general use.
1521
export const authComponent = createClient<DataModel, typeof authSchema>(
@@ -18,6 +24,24 @@ export const authComponent = createClient<DataModel, typeof authSchema>(
1824
local: {
1925
schema: authSchema,
2026
},
27+
authFunctions,
28+
triggers: {
29+
user: {
30+
onDelete: async (ctx, doc) => {
31+
if (!doc?.userId) {
32+
return;
33+
}
34+
35+
const backups = await ctx.db
36+
.query("backups")
37+
.withIndex("by_user", (q) => q.eq("userId", doc.userId as string))
38+
.collect();
39+
for (const backup of backups) {
40+
await ctx.db.delete(backup._id);
41+
}
42+
},
43+
},
44+
},
2145
},
2246
);
2347

@@ -111,3 +135,5 @@ export const getCurrentUser = query({
111135
return authComponent.safeGetAuthUser(ctx);
112136
},
113137
});
138+
139+
export const { onCreate, onDelete, onUpdate } = authComponent.triggersApi();

0 commit comments

Comments
 (0)