Skip to content

feat: Autofill customer email from better auth session or url #204

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-ears-send.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@polar-sh/better-auth": patch
---

Autofill customer email either from url query or from session
2 changes: 1 addition & 1 deletion packages/polar-betterauth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polar-sh/better-auth",
"version": "0.1.2",
"version": "0.1.3",
"description": "Polar integration for better-auth",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
7 changes: 7 additions & 0 deletions packages/polar-betterauth/src/endpoints/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const checkout = (options: PolarOptions) =>
method: "GET",
query: z.object({
products: z.union([z.array(z.string()), z.string()]),
customerEmail: z.string().email().optional(),
}),
},
async (ctx) => {
Expand All @@ -30,6 +31,7 @@ export const checkout = (options: PolarOptions) =>

try {
const checkout = await options.client.checkouts.create({
customerEmail: ctx.query.customerEmail ?? session?.user.email,
customerExternalId: session?.user.id,
products: Array.isArray(products) ? products : [products],
successUrl: options.checkout.successUrl
Expand Down Expand Up @@ -60,8 +62,12 @@ export const checkoutWithSlug = (options: PolarOptions) =>
params: z.object({
slug: z.string(),
}),
query: z.object({
customerEmail: z.string().email().optional(),
}),
},
async (ctx) => {

if (!options.checkout?.enabled) {
throw new APIError("BAD_REQUEST", {
message: "Checkout is not enabled",
Expand Down Expand Up @@ -92,6 +98,7 @@ export const checkoutWithSlug = (options: PolarOptions) =>

try {
const checkout = await options.client.checkouts.create({
customerEmail: ctx.query.customerEmail ?? session?.user.email,
customerExternalId: session?.user.id,
products: [productId],
successUrl: options.checkout.successUrl
Expand Down
Loading