better-auth-studio 1.1.1-beta.3 → 1.1.1-beta.5

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