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" ;
26import { convex , crossDomain } from "@convex-dev/better-auth/plugins" ;
3- import { components } from "./_generated/api" ;
7+ import { components , internal } from "./_generated/api" ;
48import { DataModel } from "./_generated/dataModel" ;
59import { query } from "./_generated/server" ;
610import { betterAuth } from "better-auth" ;
@@ -10,6 +14,8 @@ import authSchema from "./betterAuth/schema";
1014
1115const 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.
1521export 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