-
Notifications
You must be signed in to change notification settings - Fork 22
feat. Add customer lifecycle hooks to betterauth integration #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Added 2 type definition for hooks and modified hooks to match better-auth stripe's hooks 1. onUserCreate : onCustomerCreate 2. onUserUpdate: onCustomerUpdate
WalkthroughThe changes integrate DodoPayments customer lifecycle callbacks into better-auth's customer hooks. New optional callbacks ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/betterauth/src/types.ts (2)
41-55: JSDoc@paramtags are misleading and| undefinedis redundant.The
@param customertag references a parameter that doesn't exist in the callback signature. The actual parameters aredata(containingDodoCustomeranduser) andctx. Also,| undefinedis redundant since the property is already optional with?./** - * A callback to run after a customer has been created - * @param customer - Customer Data - * @param DodoCustomer - DodoPayments Customer Data - * @returns + * A callback to run after a customer has been created. + * @param data - Contains DodoCustomer and user with DodoPaymentsCustomerId + * @param ctx - The endpoint context */ onCustomerCreate?: - | (( + ( data: { DodoCustomer: DodoPayments.Customer; user: User & { DodoPaymentsCustomerId: string }; }, ctx: GenericEndpointContext, - ) => Promise<void>) - | undefined; + ) => Promise<void>;
56-70: Same JSDoc and redundancy issues asonCustomerCreate.Apply the same fixes for consistency.
/** - * A callback to run after a customer has been updated - * @param customer - Customer Data - * @param DodoCustomer - DodoPayments Customer Data - * @returns + * A callback to run after a customer has been updated. + * @param data - Contains DodoCustomer and user with DodoPaymentsCustomerId + * @param ctx - The endpoint context */ onCustomerUpdate?: - | (( + ( data: { DodoCustomer: DodoPayments.Customer; user: User & { DodoPaymentsCustomerId: string }; }, ctx: GenericEndpointContext, - ) => Promise<void>) - | undefined; + ) => Promise<void>;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (2)
packages/betterauth/src/hooks/customer.ts(3 hunks)packages/betterauth/src/types.ts(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Checks
🔇 Additional comments (3)
packages/betterauth/src/hooks/customer.ts (3)
17-50:onCustomerCreateis invoked even when customer already exists.When
existingCustomer.email === user.email(lines 25-26), no creation or update occurs—the existing customer is simply reused. YetonCustomerCreateis still called at line 39-50. This may confuse consumers expecting the callback only when a customer is newly created.Consider whether this is intentional. If not, you could:
- Only invoke
onCustomerCreatewhen a new customer is actually created (theelsebranch at line 32).- Invoke
onCustomerUpdatewhen updating an existing customer (line 19-24).- Skip the callback entirely when the customer already exists unchanged.
+ let wasCreated = false; let dodoCustomer: DodoPayments.Customer | undefined; if (existingCustomer) { if (existingCustomer.email !== user.email) { dodoCustomer = await options.client.customers.update( existingCustomer.customer_id, { name: user.name, }, ); + // Optionally call onCustomerUpdate here instead } else { dodoCustomer = existingCustomer; + // No callback - customer unchanged } } else { // TODO: Add metadata to customer object via // getCustomerCreateParams option when it becomes // available in the API dodoCustomer = await options.client.customers.create({ email: user.email, name: user.name, }); + wasCreated = true; } // Call the onCustomerCreate callback if provided - if (options.onCustomerCreate && dodoCustomer) { + if (options.onCustomerCreate && dodoCustomer && wasCreated) { await options.onCustomerCreate(
79-98: LGTM!The
onCustomerUpdatecallback is correctly invoked only after a successful update operation. The guard check forupdatedCustomeris technically always truthy after the await (since exceptions would throw), but it provides safe defensive coding.
4-4: The import statement referenced in this review comment does not exist inpackages/betterauth/src/hooks/customer.ts. Line 4 containsimport type { DodoPaymentsOptions } from "../types";, not an import ofDodoPaymentsfrom thedodopaymentspackage. The file does not import or useDodoPaymentsdirectly.Likely an incorrect or invalid review comment.
📝 Description
Added 2 type definition for hooks and modified hooks to match better-auth stripe's hooks
🔄 Type of Change
Please check the type of change your PR introduces:
🎯 Affected Packages
Which packages are affected by this change:
@dodopayments/core@dodopayments/nextjs@dodopayments/express@dodopayments/fastify@dodopayments/hono@dodopayments/remix@dodopayments/sveltekit@dodopayments/astro@dodopayments/tanstack@dodopayments/nuxt@dodopayments/betterauth@dodopayments/convex📚 Documentation
🔍 Code Quality
🚀 Framework Adapter Checklist
If you're adding a new framework adapter, please ensure:
🔐 Security Considerations
📖 Additional Context
Add any other context, screenshots, or additional information about the PR here.
Breaking Changes
Dependencies
🎯 Reviewer Notes
Specific areas to focus on during review:
📋 Pre-merge Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the GPL v3 license and that I have the right to submit this work under this license.
Summary by CodeRabbit
onCustomerCreate(triggered after customer creation) andonCustomerUpdate(triggered after customer updates). These callbacks enable custom handling of customer data following sign-up and profile modifications.✏️ Tip: You can customize this high-level summary in your review settings.