better-auth-studio 1.1.1-beta.4 → 1.1.1-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/adapters/hono.d.ts.map +1 -1
  2. package/dist/adapters/hono.js +5 -0
  3. package/dist/adapters/hono.js.map +1 -1
  4. package/dist/config.d.ts.map +1 -1
  5. package/dist/config.js.map +1 -1
  6. package/dist/core/handler.d.ts.map +1 -1
  7. package/dist/core/handler.js +85 -4
  8. package/dist/core/handler.js.map +1 -1
  9. package/dist/index.d.ts +4 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +3 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/providers/events/helpers.d.ts +22 -0
  14. package/dist/providers/events/helpers.d.ts.map +1 -0
  15. package/dist/providers/events/helpers.js +874 -0
  16. package/dist/providers/events/helpers.js.map +1 -0
  17. package/dist/public/assets/main-RoeYO1I-.css +1 -0
  18. package/dist/public/assets/{main-BDwnIMk3.js → main-wiXkwSx1.js} +82 -82
  19. package/dist/public/index.html +2 -2
  20. package/dist/routes.d.ts.map +1 -1
  21. package/dist/routes.js +181 -0
  22. package/dist/routes.js.map +1 -1
  23. package/dist/studio.d.ts.map +1 -1
  24. package/dist/studio.js +78 -2
  25. package/dist/studio.js.map +1 -1
  26. package/dist/types/events.d.ts +43 -0
  27. package/dist/types/events.d.ts.map +1 -0
  28. package/dist/types/events.js +306 -0
  29. package/dist/types/events.js.map +1 -0
  30. package/dist/types/handler.d.ts +31 -0
  31. package/dist/types/handler.d.ts.map +1 -1
  32. package/dist/types/handler.js.map +1 -1
  33. package/dist/utils/auth-callbacks-injector.d.ts +3 -0
  34. package/dist/utils/auth-callbacks-injector.d.ts.map +1 -0
  35. package/dist/utils/auth-callbacks-injector.js +225 -0
  36. package/dist/utils/auth-callbacks-injector.js.map +1 -0
  37. package/dist/utils/auth-callbacks-wrapper.d.ts +7 -0
  38. package/dist/utils/auth-callbacks-wrapper.d.ts.map +1 -0
  39. package/dist/utils/auth-callbacks-wrapper.js +123 -0
  40. package/dist/utils/auth-callbacks-wrapper.js.map +1 -0
  41. package/dist/utils/database-hook-injector.d.ts +3 -0
  42. package/dist/utils/database-hook-injector.d.ts.map +1 -0
  43. package/dist/utils/database-hook-injector.js +141 -0
  44. package/dist/utils/database-hook-injector.js.map +1 -0
  45. package/dist/utils/email-otp-hooks-injector.d.ts +29 -0
  46. package/dist/utils/email-otp-hooks-injector.d.ts.map +1 -0
  47. package/dist/utils/email-otp-hooks-injector.js +134 -0
  48. package/dist/utils/email-otp-hooks-injector.js.map +1 -0
  49. package/dist/utils/event-ingestion.d.ts +38 -0
  50. package/dist/utils/event-ingestion.d.ts.map +1 -0
  51. package/dist/utils/event-ingestion.js +169 -0
  52. package/dist/utils/event-ingestion.js.map +1 -0
  53. package/dist/utils/hook-injector.d.ts +9 -0
  54. package/dist/utils/hook-injector.d.ts.map +1 -0
  55. package/dist/utils/hook-injector.js +592 -0
  56. package/dist/utils/hook-injector.js.map +1 -0
  57. package/dist/utils/html-injector.d.ts +17 -0
  58. package/dist/utils/html-injector.d.ts.map +1 -1
  59. package/dist/utils/html-injector.js +15 -1
  60. package/dist/utils/html-injector.js.map +1 -1
  61. package/dist/utils/org-hooks-injector.d.ts +74 -0
  62. package/dist/utils/org-hooks-injector.d.ts.map +1 -0
  63. package/dist/utils/org-hooks-injector.js +648 -0
  64. package/dist/utils/org-hooks-injector.js.map +1 -0
  65. package/dist/utils/org-hooks-wrapper.d.ts +74 -0
  66. package/dist/utils/org-hooks-wrapper.d.ts.map +1 -0
  67. package/dist/utils/org-hooks-wrapper.js +687 -0
  68. package/dist/utils/org-hooks-wrapper.js.map +1 -0
  69. package/dist/utils/organization-hooks-wrapper.d.ts +38 -0
  70. package/dist/utils/organization-hooks-wrapper.d.ts.map +1 -0
  71. package/dist/utils/organization-hooks-wrapper.js +297 -0
  72. package/dist/utils/organization-hooks-wrapper.js.map +1 -0
  73. package/package.json +3 -3
  74. package/public/assets/main-RoeYO1I-.css +1 -0
  75. package/public/assets/{main-BDwnIMk3.js → main-wiXkwSx1.js} +82 -82
  76. package/public/index.html +2 -2
  77. package/dist/public/assets/main-s8HrXBxq.css +0 -1
  78. package/public/assets/main-s8HrXBxq.css +0 -1
@@ -0,0 +1,687 @@
1
+ import { emitEvent } from './event-ingestion.js';
2
+ /**
3
+ * Wraps organization hooks to automatically emit events
4
+ * This should be used in the organization plugin's organizationHooks option
5
+ */
6
+ export function createOrganizationHooksWithEvents(eventsConfig, userHooks) {
7
+ if (!eventsConfig?.enabled) {
8
+ return userHooks || {};
9
+ }
10
+ const capturedConfig = eventsConfig;
11
+ // Helper to extract request info
12
+ // Note: Organization hooks don't receive request directly, so we return empty info
13
+ // The request info will be captured by the main hook injector for endpoint-based operations
14
+ const getRequestInfo = () => {
15
+ return { headers: {}, ip: undefined };
16
+ };
17
+ return {
18
+ // Organization hooks
19
+ beforeCreateOrganization: userHooks?.beforeCreateOrganization
20
+ ? async (data) => {
21
+ const result = await userHooks.beforeCreateOrganization(data);
22
+ return result;
23
+ }
24
+ : undefined,
25
+ afterCreateOrganization: userHooks?.afterCreateOrganization
26
+ ? async (data) => {
27
+ await userHooks.afterCreateOrganization(data);
28
+ // Emit event
29
+ emitEvent('organization.created', {
30
+ status: 'success',
31
+ organizationId: data.organization.id,
32
+ userId: data.user.id,
33
+ metadata: {
34
+ organizationName: data.organization.name,
35
+ organizationSlug: data.organization.slug,
36
+ email: data.user.email,
37
+ name: data.user.name,
38
+ },
39
+ request: getRequestInfo(),
40
+ }, capturedConfig).catch(() => { });
41
+ }
42
+ : async (data) => {
43
+ // Emit event even if no user hook
44
+ emitEvent('organization.created', {
45
+ status: 'success',
46
+ organizationId: data.organization.id,
47
+ userId: data.user.id,
48
+ metadata: {
49
+ organizationName: data.organization.name,
50
+ organizationSlug: data.organization.slug,
51
+ email: data.user.email,
52
+ name: data.user.name,
53
+ },
54
+ request: getRequestInfo(),
55
+ }, capturedConfig).catch(() => { });
56
+ },
57
+ beforeUpdateOrganization: userHooks?.beforeUpdateOrganization
58
+ ? async (data) => {
59
+ const result = await userHooks.beforeUpdateOrganization(data);
60
+ return result;
61
+ }
62
+ : undefined,
63
+ afterUpdateOrganization: userHooks?.afterUpdateOrganization
64
+ ? async (data) => {
65
+ await userHooks.afterUpdateOrganization?.(data);
66
+ // Emit event
67
+ if (data.organization) {
68
+ emitEvent('organization.updated', {
69
+ status: 'success',
70
+ organizationId: data.organization.id,
71
+ userId: data.user.id,
72
+ metadata: {
73
+ organizationName: data.organization.name,
74
+ organizationSlug: data.organization.slug,
75
+ email: data.user.email,
76
+ name: data.user.name,
77
+ },
78
+ request: getRequestInfo(),
79
+ }, capturedConfig).catch(() => { });
80
+ }
81
+ }
82
+ : async (data) => {
83
+ // Emit event even if no user hook
84
+ if (data.organization) {
85
+ emitEvent('organization.updated', {
86
+ status: 'success',
87
+ organizationId: data.organization.id,
88
+ userId: data.user.id,
89
+ metadata: {
90
+ organizationName: data.organization.name,
91
+ organizationSlug: data.organization.slug,
92
+ email: data.user.email,
93
+ name: data.user.name,
94
+ },
95
+ request: getRequestInfo(),
96
+ }, capturedConfig).catch(() => { });
97
+ }
98
+ },
99
+ beforeDeleteOrganization: userHooks?.beforeDeleteOrganization
100
+ ? async (data) => {
101
+ await userHooks.beforeDeleteOrganization(data);
102
+ }
103
+ : undefined,
104
+ afterDeleteOrganization: userHooks?.afterDeleteOrganization
105
+ ? async (data) => {
106
+ await userHooks.afterDeleteOrganization?.(data);
107
+ // Emit event
108
+ emitEvent('organization.deleted', {
109
+ status: 'success',
110
+ organizationId: data.organization.id,
111
+ userId: data.user.id,
112
+ metadata: {
113
+ organizationName: data.organization.name,
114
+ organizationSlug: data.organization.slug,
115
+ email: data.user.email,
116
+ name: data.user.name,
117
+ },
118
+ request: getRequestInfo(),
119
+ }, capturedConfig).catch(() => { });
120
+ }
121
+ : async (data) => {
122
+ // Emit event even if no user hook
123
+ emitEvent('organization.deleted', {
124
+ status: 'success',
125
+ organizationId: data.organization.id,
126
+ userId: data.user.id,
127
+ metadata: {
128
+ organizationName: data.organization.name,
129
+ organizationSlug: data.organization.slug,
130
+ email: data.user.email,
131
+ name: data.user.name,
132
+ },
133
+ request: getRequestInfo(),
134
+ }, capturedConfig).catch(() => { });
135
+ },
136
+ // Member hooks
137
+ beforeAddMember: userHooks?.beforeAddMember
138
+ ? async (data) => {
139
+ const result = await userHooks.beforeAddMember(data);
140
+ return result;
141
+ }
142
+ : undefined,
143
+ afterAddMember: userHooks?.afterAddMember
144
+ ? async (data) => {
145
+ await userHooks.afterAddMember?.(data);
146
+ // Emit event
147
+ emitEvent('member.added', {
148
+ status: 'success',
149
+ organizationId: data.organization.id,
150
+ userId: data.member.userId,
151
+ metadata: {
152
+ memberId: data.member.id,
153
+ role: data.member.role,
154
+ addedByUserId: data.user.id,
155
+ addedByEmail: data.user.email,
156
+ addedByName: data.user.name,
157
+ memberEmail: data.user.email,
158
+ memberName: data.user.name,
159
+ },
160
+ request: getRequestInfo(),
161
+ }, capturedConfig).catch(() => { });
162
+ }
163
+ : async (data) => {
164
+ // Emit event even if no user hook
165
+ emitEvent('member.added', {
166
+ status: 'success',
167
+ organizationId: data.organization.id,
168
+ userId: data.member.userId,
169
+ metadata: {
170
+ memberId: data.member.id,
171
+ role: data.member.role,
172
+ addedByUserId: data.user.id,
173
+ addedByEmail: data.user.email,
174
+ addedByName: data.user.name,
175
+ memberEmail: data.user.email,
176
+ memberName: data.user.name,
177
+ },
178
+ request: getRequestInfo(),
179
+ }, capturedConfig).catch(() => { });
180
+ },
181
+ beforeRemoveMember: userHooks?.beforeRemoveMember
182
+ ? async (data) => {
183
+ await userHooks.beforeRemoveMember(data);
184
+ }
185
+ : undefined,
186
+ afterRemoveMember: userHooks?.afterRemoveMember
187
+ ? async (data) => {
188
+ await userHooks.afterRemoveMember?.(data);
189
+ // Emit event
190
+ emitEvent('member.removed', {
191
+ status: 'success',
192
+ organizationId: data.organization.id,
193
+ userId: data.member.userId,
194
+ metadata: {
195
+ memberId: data.member.id,
196
+ removedByUserId: data.user.id,
197
+ removedByEmail: data.user.email,
198
+ removedByName: data.user.name,
199
+ },
200
+ request: getRequestInfo(),
201
+ }, capturedConfig).catch(() => { });
202
+ }
203
+ : async (data) => {
204
+ // Emit event even if no user hook
205
+ emitEvent('member.removed', {
206
+ status: 'success',
207
+ organizationId: data.organization.id,
208
+ userId: data.member.userId,
209
+ metadata: {
210
+ memberId: data.member.id,
211
+ removedByUserId: data.user.id,
212
+ removedByEmail: data.user.email,
213
+ removedByName: data.user.name,
214
+ },
215
+ request: getRequestInfo(),
216
+ }, capturedConfig).catch(() => { });
217
+ },
218
+ beforeUpdateMemberRole: userHooks?.beforeUpdateMemberRole
219
+ ? async (data) => {
220
+ const result = await userHooks.beforeUpdateMemberRole(data);
221
+ return result;
222
+ }
223
+ : undefined,
224
+ afterUpdateMemberRole: userHooks?.afterUpdateMemberRole
225
+ ? async (data) => {
226
+ await userHooks.afterUpdateMemberRole?.(data);
227
+ // Emit event
228
+ emitEvent('member.role_changed', {
229
+ status: 'success',
230
+ organizationId: data.organization.id,
231
+ userId: data.member.userId,
232
+ metadata: {
233
+ memberId: data.member.id,
234
+ oldRole: data.previousRole,
235
+ newRole: data.member.role,
236
+ changedByUserId: data.user.id,
237
+ changedByEmail: data.user.email,
238
+ changedByName: data.user.name,
239
+ },
240
+ request: getRequestInfo(),
241
+ }, capturedConfig).catch(() => { });
242
+ }
243
+ : async (data) => {
244
+ // Emit event even if no user hook
245
+ emitEvent('member.role_changed', {
246
+ status: 'success',
247
+ organizationId: data.organization.id,
248
+ userId: data.member.userId,
249
+ metadata: {
250
+ memberId: data.member.id,
251
+ oldRole: data.previousRole,
252
+ newRole: data.member.role,
253
+ changedByUserId: data.user.id,
254
+ changedByEmail: data.user.email,
255
+ changedByName: data.user.name,
256
+ },
257
+ request: getRequestInfo(),
258
+ }, capturedConfig).catch(() => { });
259
+ },
260
+ // Team hooks
261
+ beforeCreateTeam: userHooks?.beforeCreateTeam
262
+ ? async (data) => {
263
+ const result = await userHooks.beforeCreateTeam(data);
264
+ return result;
265
+ }
266
+ : undefined,
267
+ afterCreateTeam: userHooks?.afterCreateTeam
268
+ ? async (data) => {
269
+ await userHooks.afterCreateTeam?.(data);
270
+ // Emit event
271
+ emitEvent('team.created', {
272
+ status: 'success',
273
+ organizationId: data.organization.id,
274
+ userId: data.user?.id,
275
+ metadata: {
276
+ teamId: data.team.id,
277
+ teamName: data.team.name,
278
+ organizationName: data.organization.name,
279
+ organizationSlug: data.organization.slug,
280
+ email: data.user?.email,
281
+ name: data.user?.name,
282
+ },
283
+ request: getRequestInfo(),
284
+ }, capturedConfig).catch(() => { });
285
+ }
286
+ : async (data) => {
287
+ // Emit event even if no user hook
288
+ emitEvent('team.created', {
289
+ status: 'success',
290
+ organizationId: data.organization.id,
291
+ userId: data.user?.id,
292
+ metadata: {
293
+ teamId: data.team.id,
294
+ teamName: data.team.name,
295
+ organizationName: data.organization.name,
296
+ organizationSlug: data.organization.slug,
297
+ email: data.user?.email,
298
+ name: data.user?.name,
299
+ },
300
+ request: getRequestInfo(),
301
+ }, capturedConfig).catch(() => { });
302
+ },
303
+ beforeUpdateTeam: userHooks?.beforeUpdateTeam
304
+ ? async (data) => {
305
+ const result = await userHooks.beforeUpdateTeam(data);
306
+ return result;
307
+ }
308
+ : undefined,
309
+ afterUpdateTeam: userHooks?.afterUpdateTeam
310
+ ? async (data) => {
311
+ await userHooks.afterUpdateTeam?.(data);
312
+ // Emit event
313
+ if (data.team) {
314
+ emitEvent('team.updated', {
315
+ status: 'success',
316
+ organizationId: data.organization.id,
317
+ userId: data.user.id,
318
+ metadata: {
319
+ teamId: data.team.id,
320
+ teamName: data.team.name,
321
+ organizationName: data.organization.name,
322
+ organizationSlug: data.organization.slug,
323
+ email: data.user.email,
324
+ name: data.user.name,
325
+ },
326
+ request: getRequestInfo(),
327
+ }, capturedConfig).catch(() => { });
328
+ }
329
+ }
330
+ : async (data) => {
331
+ // Emit event even if no user hook
332
+ if (data.team) {
333
+ emitEvent('team.updated', {
334
+ status: 'success',
335
+ organizationId: data.organization.id,
336
+ userId: data.user.id,
337
+ metadata: {
338
+ teamId: data.team.id,
339
+ teamName: data.team.name,
340
+ organizationName: data.organization.name,
341
+ organizationSlug: data.organization.slug,
342
+ email: data.user.email,
343
+ name: data.user.name,
344
+ },
345
+ request: getRequestInfo(),
346
+ }, capturedConfig).catch(() => { });
347
+ }
348
+ },
349
+ beforeDeleteTeam: userHooks?.beforeDeleteTeam
350
+ ? async (data) => {
351
+ await userHooks.beforeDeleteTeam(data);
352
+ }
353
+ : undefined,
354
+ afterDeleteTeam: userHooks?.afterDeleteTeam
355
+ ? async (data) => {
356
+ await userHooks.afterDeleteTeam?.(data);
357
+ // Emit event
358
+ emitEvent('team.deleted', {
359
+ status: 'success',
360
+ organizationId: data.organization.id,
361
+ userId: data.user?.id,
362
+ metadata: {
363
+ teamId: data.team.id,
364
+ teamName: data.team.name,
365
+ organizationName: data.organization.name,
366
+ organizationSlug: data.organization.slug,
367
+ email: data.user?.email,
368
+ name: data.user?.name,
369
+ },
370
+ request: getRequestInfo(),
371
+ }, capturedConfig).catch(() => { });
372
+ }
373
+ : async (data) => {
374
+ // Emit event even if no user hook
375
+ emitEvent('team.deleted', {
376
+ status: 'success',
377
+ organizationId: data.organization.id,
378
+ userId: data.user?.id,
379
+ metadata: {
380
+ teamId: data.team.id,
381
+ teamName: data.team.name,
382
+ organizationName: data.organization.name,
383
+ organizationSlug: data.organization.slug,
384
+ email: data.user?.email,
385
+ name: data.user?.name,
386
+ },
387
+ request: getRequestInfo(),
388
+ }, capturedConfig).catch(() => { });
389
+ },
390
+ // Team member hooks
391
+ beforeAddTeamMember: userHooks?.beforeAddTeamMember
392
+ ? async (data) => {
393
+ const result = await userHooks.beforeAddTeamMember(data);
394
+ return result;
395
+ }
396
+ : undefined,
397
+ afterAddTeamMember: userHooks?.afterAddTeamMember
398
+ ? async (data) => {
399
+ await userHooks.afterAddTeamMember?.(data);
400
+ // Emit event
401
+ emitEvent('team.member.added', {
402
+ status: 'success',
403
+ organizationId: data.organization.id,
404
+ userId: data.teamMember.userId,
405
+ metadata: {
406
+ teamMemberId: data.teamMember.id,
407
+ teamId: data.team.id,
408
+ teamName: data.team.name,
409
+ organizationName: data.organization.name,
410
+ memberEmail: data.user.email,
411
+ memberName: data.user.name,
412
+ addedByUserId: data.user.id,
413
+ addedByEmail: data.user.email,
414
+ addedByName: data.user.name,
415
+ },
416
+ request: getRequestInfo(),
417
+ }, capturedConfig).catch(() => { });
418
+ }
419
+ : async (data) => {
420
+ // Emit event even if no user hook
421
+ emitEvent('team.member.added', {
422
+ status: 'success',
423
+ organizationId: data.organization.id,
424
+ userId: data.teamMember.userId,
425
+ metadata: {
426
+ teamMemberId: data.teamMember.id,
427
+ teamId: data.team.id,
428
+ teamName: data.team.name,
429
+ organizationName: data.organization.name,
430
+ memberEmail: data.user.email,
431
+ memberName: data.user.name,
432
+ addedUserId: data.user.id,
433
+ addedEmail: data.user.email,
434
+ addedName: data.user.name,
435
+ },
436
+ request: getRequestInfo(),
437
+ }, capturedConfig).catch(() => { });
438
+ },
439
+ beforeRemoveTeamMember: userHooks?.beforeRemoveTeamMember
440
+ ? async (data) => {
441
+ await userHooks.beforeRemoveTeamMember(data);
442
+ }
443
+ : undefined,
444
+ afterRemoveTeamMember: userHooks?.afterRemoveTeamMember
445
+ ? async (data) => {
446
+ await userHooks.afterRemoveTeamMember?.(data);
447
+ // Emit event
448
+ emitEvent('team.member.removed', {
449
+ status: 'success',
450
+ organizationId: data.organization.id,
451
+ userId: data.teamMember.userId,
452
+ metadata: {
453
+ teamMemberId: data.teamMember.id,
454
+ teamId: data.team.id,
455
+ teamName: data.team.name,
456
+ organizationName: data.organization.name,
457
+ removedUserId: data.user.id,
458
+ removedEmail: data.user.email,
459
+ removedName: data.user.name,
460
+ },
461
+ request: getRequestInfo(),
462
+ }, capturedConfig).catch(() => { });
463
+ }
464
+ : async (data) => {
465
+ // Emit event even if no user hook
466
+ emitEvent('team.member.removed', {
467
+ status: 'success',
468
+ organizationId: data.organization.id,
469
+ userId: data.teamMember.userId,
470
+ metadata: {
471
+ teamMemberId: data.teamMember.id,
472
+ teamId: data.team.id,
473
+ teamName: data.team.name,
474
+ organizationName: data.organization.name,
475
+ removedUserId: data.user.id,
476
+ removedEmail: data.user.email,
477
+ removedName: data.user.name,
478
+ },
479
+ request: getRequestInfo(),
480
+ }, capturedConfig).catch(() => { });
481
+ },
482
+ // Invitation hooks
483
+ beforeCreateInvitation: userHooks?.beforeCreateInvitation
484
+ ? async (data) => {
485
+ const result = await userHooks.beforeCreateInvitation(data);
486
+ return result;
487
+ }
488
+ : undefined,
489
+ afterCreateInvitation: userHooks?.afterCreateInvitation
490
+ ? async (data) => {
491
+ await userHooks.afterCreateInvitation?.(data);
492
+ // Emit event
493
+ emitEvent('invitation.created', {
494
+ status: 'success',
495
+ organizationId: data.organization.id,
496
+ metadata: {
497
+ invitationId: data.invitation.id,
498
+ email: data.invitation.email,
499
+ role: data.invitation.role,
500
+ organizationName: data.organization.name,
501
+ organizationSlug: data.organization.slug,
502
+ inviterEmail: data.inviter.email,
503
+ inviterName: data.inviter.name,
504
+ inviterId: data.inviter.id,
505
+ teamId: data.invitation.teamId,
506
+ },
507
+ request: getRequestInfo(),
508
+ }, capturedConfig).catch(() => { });
509
+ }
510
+ : async (data) => {
511
+ // Emit event even if no user hook
512
+ emitEvent('invitation.created', {
513
+ status: 'success',
514
+ organizationId: data.organization.id,
515
+ metadata: {
516
+ invitationId: data.invitation.id,
517
+ email: data.invitation.email,
518
+ role: data.invitation.role,
519
+ organizationName: data.organization.name,
520
+ organizationSlug: data.organization.slug,
521
+ inviterEmail: data.inviter.email,
522
+ inviterName: data.inviter.name,
523
+ inviterId: data.inviter.id,
524
+ teamId: data.invitation.teamId,
525
+ },
526
+ request: getRequestInfo(),
527
+ }, capturedConfig).catch(() => { });
528
+ },
529
+ beforeAcceptInvitation: userHooks?.beforeAcceptInvitation
530
+ ? async (data) => {
531
+ await userHooks.beforeAcceptInvitation(data);
532
+ }
533
+ : undefined,
534
+ afterAcceptInvitation: userHooks?.afterAcceptInvitation
535
+ ? async (data) => {
536
+ await userHooks.afterAcceptInvitation?.(data);
537
+ // Emit event
538
+ emitEvent('invitation.accepted', {
539
+ status: 'success',
540
+ organizationId: data.organization.id,
541
+ userId: data.user.id,
542
+ metadata: {
543
+ invitationId: data.invitation.id,
544
+ email: data.user.email,
545
+ name: data.user.name,
546
+ role: data.member.role,
547
+ organizationName: data.organization.name,
548
+ organizationSlug: data.organization.slug,
549
+ },
550
+ request: getRequestInfo(),
551
+ }, capturedConfig).catch(() => { });
552
+ }
553
+ : async (data) => {
554
+ // Emit event even if no user hook
555
+ emitEvent('invitation.accepted', {
556
+ status: 'success',
557
+ organizationId: data.organization.id,
558
+ userId: data.user.id,
559
+ metadata: {
560
+ invitationId: data.invitation.id,
561
+ email: data.user.email,
562
+ name: data.user.name,
563
+ role: data.member.role,
564
+ organizationName: data.organization.name,
565
+ organizationSlug: data.organization.slug,
566
+ },
567
+ request: getRequestInfo(),
568
+ }, capturedConfig).catch(() => { });
569
+ },
570
+ beforeRejectInvitation: userHooks?.beforeRejectInvitation
571
+ ? async (data) => {
572
+ await userHooks.beforeRejectInvitation(data);
573
+ }
574
+ : undefined,
575
+ afterRejectInvitation: userHooks?.afterRejectInvitation
576
+ ? async (data) => {
577
+ await userHooks.afterRejectInvitation?.(data);
578
+ // Emit event
579
+ emitEvent('invitation.rejected', {
580
+ status: 'success',
581
+ organizationId: data.organization.id,
582
+ userId: data.user.id,
583
+ metadata: {
584
+ invitationId: data.invitation.id,
585
+ email: data.user.email,
586
+ name: data.user.name,
587
+ organizationName: data.organization.name,
588
+ organizationSlug: data.organization.slug,
589
+ },
590
+ request: getRequestInfo(),
591
+ }, capturedConfig).catch(() => { });
592
+ }
593
+ : async (data) => {
594
+ // Emit event even if no user hook
595
+ emitEvent('invitation.rejected', {
596
+ status: 'success',
597
+ organizationId: data.organization.id,
598
+ userId: data.user.id,
599
+ metadata: {
600
+ invitationId: data.invitation.id,
601
+ email: data.user.email,
602
+ name: data.user.name,
603
+ organizationName: data.organization.name,
604
+ organizationSlug: data.organization.slug,
605
+ },
606
+ request: getRequestInfo(),
607
+ }, capturedConfig).catch(() => { });
608
+ },
609
+ beforeCancelInvitation: userHooks?.beforeCancelInvitation
610
+ ? async (data) => {
611
+ await userHooks.beforeCancelInvitation(data);
612
+ }
613
+ : undefined,
614
+ afterCancelInvitation: userHooks?.afterCancelInvitation
615
+ ? async (data) => {
616
+ await userHooks.afterCancelInvitation?.(data);
617
+ // Emit event
618
+ emitEvent('invitation.cancelled', {
619
+ status: 'success',
620
+ organizationId: data.organization.id,
621
+ userId: data.cancelledBy.id,
622
+ metadata: {
623
+ invitationId: data.invitation.id,
624
+ email: data.invitation.email,
625
+ organizationName: data.organization.name,
626
+ organizationSlug: data.organization.slug,
627
+ cancelledByEmail: data.cancelledBy.email,
628
+ cancelledByName: data.cancelledBy.name,
629
+ },
630
+ request: getRequestInfo(),
631
+ }, capturedConfig).catch(() => { });
632
+ }
633
+ : async (data) => {
634
+ // Emit event even if no user hook
635
+ emitEvent('invitation.cancelled', {
636
+ status: 'success',
637
+ organizationId: data.organization.id,
638
+ userId: data.cancelledBy.id,
639
+ metadata: {
640
+ invitationId: data.invitation.id,
641
+ email: data.invitation.email,
642
+ organizationName: data.organization.name,
643
+ organizationSlug: data.organization.slug,
644
+ cancelledByEmail: data.cancelledBy.email,
645
+ cancelledByName: data.cancelledBy.name,
646
+ },
647
+ request: getRequestInfo(),
648
+ }, capturedConfig).catch(() => { });
649
+ },
650
+ };
651
+ }
652
+ /**
653
+ * Automatically wraps organization plugin hooks to emit events
654
+ * This should be called during Better Auth initialization
655
+ */
656
+ export function wrapOrganizationPluginHooks(auth, eventsConfig) {
657
+ if (!auth || !eventsConfig?.enabled) {
658
+ return;
659
+ }
660
+ try {
661
+ // Find the organization plugin
662
+ const plugins = auth.options?.plugins || [];
663
+ const orgPlugin = plugins.find((p) => p?.id === 'organization');
664
+ if (!orgPlugin) {
665
+ return; // Organization plugin not found
666
+ }
667
+ // Get existing organization hooks from plugin options
668
+ // Better Auth may store hooks in different locations depending on version
669
+ const existingHooks = orgPlugin.options?.organizationHooks ||
670
+ orgPlugin.organizationHooks ||
671
+ (orgPlugin.options && orgPlugin.options.organizationHooks) ||
672
+ {};
673
+ // Wrap the hooks
674
+ const wrappedHooks = createOrganizationHooksWithEvents(eventsConfig, existingHooks);
675
+ // Update the plugin options in all possible locations
676
+ if (!orgPlugin.options) {
677
+ orgPlugin.options = {};
678
+ }
679
+ orgPlugin.options.organizationHooks = wrappedHooks;
680
+ // Also set directly on plugin for compatibility
681
+ orgPlugin.organizationHooks = wrappedHooks;
682
+ }
683
+ catch (error) {
684
+ console.error('[Organization Hooks] Failed to wrap hooks:', error);
685
+ }
686
+ }
687
+ //# sourceMappingURL=org-hooks-wrapper.js.map