@polar-sh/better-auth 1.6.4 → 1.8.0

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.
package/dist/index.d.cts CHANGED
@@ -63,22 +63,24 @@ interface CheckoutOptions {
63
63
  * Checkout theme
64
64
  */
65
65
  theme?: "light" | "dark";
66
- /**
67
- * Redirect to checkout page
68
- */
69
- redirect?: boolean;
70
66
  }
71
67
  declare const CheckoutParams: z.ZodObject<{
72
68
  products: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString]>>;
73
69
  slug: z.ZodOptional<z.ZodString>;
74
70
  referenceId: z.ZodOptional<z.ZodString>;
75
71
  customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
76
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
72
+ metadata: z.ZodOptional<z.ZodEffects<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
77
73
  allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
78
74
  discountId: z.ZodOptional<z.ZodString>;
79
75
  redirect: z.ZodOptional<z.ZodBoolean>;
80
76
  embedOrigin: z.ZodOptional<z.ZodString>;
77
+ successUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
78
+ returnUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
79
+ allowTrial: z.ZodOptional<z.ZodBoolean>;
80
+ trialInterval: z.ZodOptional<z.ZodEnum<["day", "week", "month", "year"]>>;
81
+ trialIntervalCount: z.ZodOptional<z.ZodNumber>;
81
82
  }, "strip", z.ZodTypeAny, {
83
+ returnUrl?: string | undefined;
82
84
  products?: string | string[] | undefined;
83
85
  slug?: string | undefined;
84
86
  referenceId?: string | undefined;
@@ -88,7 +90,12 @@ declare const CheckoutParams: z.ZodObject<{
88
90
  discountId?: string | undefined;
89
91
  redirect?: boolean | undefined;
90
92
  embedOrigin?: string | undefined;
93
+ successUrl?: string | undefined;
94
+ allowTrial?: boolean | undefined;
95
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
96
+ trialIntervalCount?: number | undefined;
91
97
  }, {
98
+ returnUrl?: string | undefined;
92
99
  products?: string | string[] | undefined;
93
100
  slug?: string | undefined;
94
101
  referenceId?: string | undefined;
@@ -98,6 +105,10 @@ declare const CheckoutParams: z.ZodObject<{
98
105
  discountId?: string | undefined;
99
106
  redirect?: boolean | undefined;
100
107
  embedOrigin?: string | undefined;
108
+ successUrl?: string | undefined;
109
+ allowTrial?: boolean | undefined;
110
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
111
+ trialIntervalCount?: number | undefined;
101
112
  }>;
102
113
  type CheckoutParams = z.infer<typeof CheckoutParams>;
103
114
  declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
@@ -108,12 +119,18 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
108
119
  slug: z.ZodOptional<z.ZodString>;
109
120
  referenceId: z.ZodOptional<z.ZodString>;
110
121
  customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
111
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
122
+ metadata: z.ZodOptional<z.ZodEffects<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
112
123
  allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
113
124
  discountId: z.ZodOptional<z.ZodString>;
114
125
  redirect: z.ZodOptional<z.ZodBoolean>;
115
126
  embedOrigin: z.ZodOptional<z.ZodString>;
127
+ successUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
128
+ returnUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
129
+ allowTrial: z.ZodOptional<z.ZodBoolean>;
130
+ trialInterval: z.ZodOptional<z.ZodEnum<["day", "week", "month", "year"]>>;
131
+ trialIntervalCount: z.ZodOptional<z.ZodNumber>;
116
132
  }, "strip", z.ZodTypeAny, {
133
+ returnUrl?: string | undefined;
117
134
  products?: string | string[] | undefined;
118
135
  slug?: string | undefined;
119
136
  referenceId?: string | undefined;
@@ -123,7 +140,12 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
123
140
  discountId?: string | undefined;
124
141
  redirect?: boolean | undefined;
125
142
  embedOrigin?: string | undefined;
143
+ successUrl?: string | undefined;
144
+ allowTrial?: boolean | undefined;
145
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
146
+ trialIntervalCount?: number | undefined;
126
147
  }, {
148
+ returnUrl?: string | undefined;
127
149
  products?: string | string[] | undefined;
128
150
  slug?: string | undefined;
129
151
  referenceId?: string | undefined;
@@ -133,6 +155,10 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
133
155
  discountId?: string | undefined;
134
156
  redirect?: boolean | undefined;
135
157
  embedOrigin?: string | undefined;
158
+ successUrl?: string | undefined;
159
+ allowTrial?: boolean | undefined;
160
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
161
+ trialIntervalCount?: number | undefined;
136
162
  }>;
137
163
  }, {
138
164
  url: string;
@@ -142,10 +168,21 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
142
168
 
143
169
  interface PortalConfig {
144
170
  returnUrl?: string;
171
+ /**
172
+ * Portal theme
173
+ */
174
+ theme?: "light" | "dark";
145
175
  }
146
- declare const portal: ({ returnUrl }?: PortalConfig) => (polar: Polar) => {
176
+ declare const portal: ({ returnUrl, theme }?: PortalConfig) => (polar: Polar) => {
147
177
  portal: better_auth.StrictEndpoint<"/customer/portal", {
148
- method: "GET";
178
+ method: ("POST" | "GET")[];
179
+ body: z.ZodOptional<z.ZodObject<{
180
+ redirect: z.ZodOptional<z.ZodBoolean>;
181
+ }, "strip", z.ZodTypeAny, {
182
+ redirect?: boolean | undefined;
183
+ }, {
184
+ redirect?: boolean | undefined;
185
+ }>>;
149
186
  use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
150
187
  session: {
151
188
  session: Record<string, any> & {
@@ -583,12 +620,18 @@ declare const polar: <O extends PolarOptions>(options: O) => {
583
620
  slug: zod.ZodOptional<zod.ZodString>;
584
621
  referenceId: zod.ZodOptional<zod.ZodString>;
585
622
  customFieldData: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
586
- metadata: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
623
+ metadata: zod.ZodOptional<zod.ZodEffects<zod.ZodEffects<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
587
624
  allowDiscountCodes: zod.ZodOptional<zod.ZodBoolean>;
588
625
  discountId: zod.ZodOptional<zod.ZodString>;
589
626
  redirect: zod.ZodOptional<zod.ZodBoolean>;
590
627
  embedOrigin: zod.ZodOptional<zod.ZodString>;
628
+ successUrl: zod.ZodOptional<zod.ZodEffects<zod.ZodString, string, string>>;
629
+ returnUrl: zod.ZodOptional<zod.ZodEffects<zod.ZodString, string, string>>;
630
+ allowTrial: zod.ZodOptional<zod.ZodBoolean>;
631
+ trialInterval: zod.ZodOptional<zod.ZodEnum<["day", "week", "month", "year"]>>;
632
+ trialIntervalCount: zod.ZodOptional<zod.ZodNumber>;
591
633
  }, "strip", zod.ZodTypeAny, {
634
+ returnUrl?: string | undefined;
592
635
  products?: string | string[] | undefined;
593
636
  slug?: string | undefined;
594
637
  referenceId?: string | undefined;
@@ -598,7 +641,12 @@ declare const polar: <O extends PolarOptions>(options: O) => {
598
641
  discountId?: string | undefined;
599
642
  redirect?: boolean | undefined;
600
643
  embedOrigin?: string | undefined;
644
+ successUrl?: string | undefined;
645
+ allowTrial?: boolean | undefined;
646
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
647
+ trialIntervalCount?: number | undefined;
601
648
  }, {
649
+ returnUrl?: string | undefined;
602
650
  products?: string | string[] | undefined;
603
651
  slug?: string | undefined;
604
652
  referenceId?: string | undefined;
@@ -608,6 +656,10 @@ declare const polar: <O extends PolarOptions>(options: O) => {
608
656
  discountId?: string | undefined;
609
657
  redirect?: boolean | undefined;
610
658
  embedOrigin?: string | undefined;
659
+ successUrl?: string | undefined;
660
+ allowTrial?: boolean | undefined;
661
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
662
+ trialIntervalCount?: number | undefined;
611
663
  }>;
612
664
  }, {
613
665
  url: string;
@@ -690,7 +742,14 @@ declare const polar: <O extends PolarOptions>(options: O) => {
690
742
  }, _polar_sh_sdk_models_components_eventsingestresponse_js.EventsIngestResponse>;
691
743
  } | {
692
744
  portal: better_auth.StrictEndpoint<"/customer/portal", {
693
- method: "GET";
745
+ method: ("POST" | "GET")[];
746
+ body: zod.ZodOptional<zod.ZodObject<{
747
+ redirect: zod.ZodOptional<zod.ZodBoolean>;
748
+ }, "strip", zod.ZodTypeAny, {
749
+ redirect?: boolean | undefined;
750
+ }, {
751
+ redirect?: boolean | undefined;
752
+ }>>;
694
753
  use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
695
754
  session: {
696
755
  session: Record<string, any> & {
package/dist/index.d.ts CHANGED
@@ -63,22 +63,24 @@ interface CheckoutOptions {
63
63
  * Checkout theme
64
64
  */
65
65
  theme?: "light" | "dark";
66
- /**
67
- * Redirect to checkout page
68
- */
69
- redirect?: boolean;
70
66
  }
71
67
  declare const CheckoutParams: z.ZodObject<{
72
68
  products: z.ZodOptional<z.ZodUnion<[z.ZodArray<z.ZodString, "many">, z.ZodString]>>;
73
69
  slug: z.ZodOptional<z.ZodString>;
74
70
  referenceId: z.ZodOptional<z.ZodString>;
75
71
  customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
76
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
72
+ metadata: z.ZodOptional<z.ZodEffects<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
77
73
  allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
78
74
  discountId: z.ZodOptional<z.ZodString>;
79
75
  redirect: z.ZodOptional<z.ZodBoolean>;
80
76
  embedOrigin: z.ZodOptional<z.ZodString>;
77
+ successUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
78
+ returnUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
79
+ allowTrial: z.ZodOptional<z.ZodBoolean>;
80
+ trialInterval: z.ZodOptional<z.ZodEnum<["day", "week", "month", "year"]>>;
81
+ trialIntervalCount: z.ZodOptional<z.ZodNumber>;
81
82
  }, "strip", z.ZodTypeAny, {
83
+ returnUrl?: string | undefined;
82
84
  products?: string | string[] | undefined;
83
85
  slug?: string | undefined;
84
86
  referenceId?: string | undefined;
@@ -88,7 +90,12 @@ declare const CheckoutParams: z.ZodObject<{
88
90
  discountId?: string | undefined;
89
91
  redirect?: boolean | undefined;
90
92
  embedOrigin?: string | undefined;
93
+ successUrl?: string | undefined;
94
+ allowTrial?: boolean | undefined;
95
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
96
+ trialIntervalCount?: number | undefined;
91
97
  }, {
98
+ returnUrl?: string | undefined;
92
99
  products?: string | string[] | undefined;
93
100
  slug?: string | undefined;
94
101
  referenceId?: string | undefined;
@@ -98,6 +105,10 @@ declare const CheckoutParams: z.ZodObject<{
98
105
  discountId?: string | undefined;
99
106
  redirect?: boolean | undefined;
100
107
  embedOrigin?: string | undefined;
108
+ successUrl?: string | undefined;
109
+ allowTrial?: boolean | undefined;
110
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
111
+ trialIntervalCount?: number | undefined;
101
112
  }>;
102
113
  type CheckoutParams = z.infer<typeof CheckoutParams>;
103
114
  declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) => {
@@ -108,12 +119,18 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
108
119
  slug: z.ZodOptional<z.ZodString>;
109
120
  referenceId: z.ZodOptional<z.ZodString>;
110
121
  customFieldData: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
111
- metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>;
122
+ metadata: z.ZodOptional<z.ZodEffects<z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
112
123
  allowDiscountCodes: z.ZodOptional<z.ZodBoolean>;
113
124
  discountId: z.ZodOptional<z.ZodString>;
114
125
  redirect: z.ZodOptional<z.ZodBoolean>;
115
126
  embedOrigin: z.ZodOptional<z.ZodString>;
127
+ successUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
128
+ returnUrl: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
129
+ allowTrial: z.ZodOptional<z.ZodBoolean>;
130
+ trialInterval: z.ZodOptional<z.ZodEnum<["day", "week", "month", "year"]>>;
131
+ trialIntervalCount: z.ZodOptional<z.ZodNumber>;
116
132
  }, "strip", z.ZodTypeAny, {
133
+ returnUrl?: string | undefined;
117
134
  products?: string | string[] | undefined;
118
135
  slug?: string | undefined;
119
136
  referenceId?: string | undefined;
@@ -123,7 +140,12 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
123
140
  discountId?: string | undefined;
124
141
  redirect?: boolean | undefined;
125
142
  embedOrigin?: string | undefined;
143
+ successUrl?: string | undefined;
144
+ allowTrial?: boolean | undefined;
145
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
146
+ trialIntervalCount?: number | undefined;
126
147
  }, {
148
+ returnUrl?: string | undefined;
127
149
  products?: string | string[] | undefined;
128
150
  slug?: string | undefined;
129
151
  referenceId?: string | undefined;
@@ -133,6 +155,10 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
133
155
  discountId?: string | undefined;
134
156
  redirect?: boolean | undefined;
135
157
  embedOrigin?: string | undefined;
158
+ successUrl?: string | undefined;
159
+ allowTrial?: boolean | undefined;
160
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
161
+ trialIntervalCount?: number | undefined;
136
162
  }>;
137
163
  }, {
138
164
  url: string;
@@ -142,10 +168,21 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (polar: Polar) =>
142
168
 
143
169
  interface PortalConfig {
144
170
  returnUrl?: string;
171
+ /**
172
+ * Portal theme
173
+ */
174
+ theme?: "light" | "dark";
145
175
  }
146
- declare const portal: ({ returnUrl }?: PortalConfig) => (polar: Polar) => {
176
+ declare const portal: ({ returnUrl, theme }?: PortalConfig) => (polar: Polar) => {
147
177
  portal: better_auth.StrictEndpoint<"/customer/portal", {
148
- method: "GET";
178
+ method: ("POST" | "GET")[];
179
+ body: z.ZodOptional<z.ZodObject<{
180
+ redirect: z.ZodOptional<z.ZodBoolean>;
181
+ }, "strip", z.ZodTypeAny, {
182
+ redirect?: boolean | undefined;
183
+ }, {
184
+ redirect?: boolean | undefined;
185
+ }>>;
149
186
  use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
150
187
  session: {
151
188
  session: Record<string, any> & {
@@ -583,12 +620,18 @@ declare const polar: <O extends PolarOptions>(options: O) => {
583
620
  slug: zod.ZodOptional<zod.ZodString>;
584
621
  referenceId: zod.ZodOptional<zod.ZodString>;
585
622
  customFieldData: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
586
- metadata: zod.ZodOptional<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>>;
623
+ metadata: zod.ZodOptional<zod.ZodEffects<zod.ZodEffects<zod.ZodRecord<zod.ZodString, zod.ZodUnion<[zod.ZodString, zod.ZodNumber, zod.ZodBoolean]>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>, Record<string, string | number | boolean>, Record<string, string | number | boolean>>>;
587
624
  allowDiscountCodes: zod.ZodOptional<zod.ZodBoolean>;
588
625
  discountId: zod.ZodOptional<zod.ZodString>;
589
626
  redirect: zod.ZodOptional<zod.ZodBoolean>;
590
627
  embedOrigin: zod.ZodOptional<zod.ZodString>;
628
+ successUrl: zod.ZodOptional<zod.ZodEffects<zod.ZodString, string, string>>;
629
+ returnUrl: zod.ZodOptional<zod.ZodEffects<zod.ZodString, string, string>>;
630
+ allowTrial: zod.ZodOptional<zod.ZodBoolean>;
631
+ trialInterval: zod.ZodOptional<zod.ZodEnum<["day", "week", "month", "year"]>>;
632
+ trialIntervalCount: zod.ZodOptional<zod.ZodNumber>;
591
633
  }, "strip", zod.ZodTypeAny, {
634
+ returnUrl?: string | undefined;
592
635
  products?: string | string[] | undefined;
593
636
  slug?: string | undefined;
594
637
  referenceId?: string | undefined;
@@ -598,7 +641,12 @@ declare const polar: <O extends PolarOptions>(options: O) => {
598
641
  discountId?: string | undefined;
599
642
  redirect?: boolean | undefined;
600
643
  embedOrigin?: string | undefined;
644
+ successUrl?: string | undefined;
645
+ allowTrial?: boolean | undefined;
646
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
647
+ trialIntervalCount?: number | undefined;
601
648
  }, {
649
+ returnUrl?: string | undefined;
602
650
  products?: string | string[] | undefined;
603
651
  slug?: string | undefined;
604
652
  referenceId?: string | undefined;
@@ -608,6 +656,10 @@ declare const polar: <O extends PolarOptions>(options: O) => {
608
656
  discountId?: string | undefined;
609
657
  redirect?: boolean | undefined;
610
658
  embedOrigin?: string | undefined;
659
+ successUrl?: string | undefined;
660
+ allowTrial?: boolean | undefined;
661
+ trialInterval?: "day" | "week" | "month" | "year" | undefined;
662
+ trialIntervalCount?: number | undefined;
611
663
  }>;
612
664
  }, {
613
665
  url: string;
@@ -690,7 +742,14 @@ declare const polar: <O extends PolarOptions>(options: O) => {
690
742
  }, _polar_sh_sdk_models_components_eventsingestresponse_js.EventsIngestResponse>;
691
743
  } | {
692
744
  portal: better_auth.StrictEndpoint<"/customer/portal", {
693
- method: "GET";
745
+ method: ("POST" | "GET")[];
746
+ body: zod.ZodOptional<zod.ZodObject<{
747
+ redirect: zod.ZodOptional<zod.ZodBoolean>;
748
+ }, "strip", zod.ZodTypeAny, {
749
+ redirect?: boolean | undefined;
750
+ }, {
751
+ redirect?: boolean | undefined;
752
+ }>>;
694
753
  use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
695
754
  session: {
696
755
  session: Record<string, any> & {
package/dist/index.js CHANGED
@@ -1,12 +1,15 @@
1
1
  import {
2
2
  polarClient
3
- } from "./chunk-5QR5Y5PS.js";
3
+ } from "./chunk-T2OMXYG3.js";
4
4
 
5
5
  // src/hooks/customer.ts
6
6
  import { APIError } from "better-auth/api";
7
7
  var onBeforeUserCreate = (options) => async (user, context) => {
8
8
  if (context && options.createCustomerOnSignUp) {
9
9
  try {
10
+ if (user.isAnonymous) {
11
+ return;
12
+ }
10
13
  const params = options.getCustomerCreateParams ? await options.getCustomerCreateParams({
11
14
  user
12
15
  }) : {};
@@ -38,6 +41,9 @@ var onBeforeUserCreate = (options) => async (user, context) => {
38
41
  };
39
42
  var onAfterUserCreate = (options) => async (user, context) => {
40
43
  if (context && options.createCustomerOnSignUp) {
44
+ if (user.isAnonymous) {
45
+ return;
46
+ }
41
47
  try {
42
48
  const { result: existingCustomers } = await options.client.customers.list({ email: user.email });
43
49
  const existingCustomer = existingCustomers.items[0];
@@ -66,6 +72,9 @@ var onAfterUserCreate = (options) => async (user, context) => {
66
72
  var onUserUpdate = (options) => async (user, context) => {
67
73
  if (context && options.createCustomerOnSignUp) {
68
74
  try {
75
+ if (user.isAnonymous) {
76
+ return;
77
+ }
69
78
  await options.client.customers.updateExternal({
70
79
  externalId: user.id,
71
80
  customerUpdateExternalID: {
@@ -89,6 +98,9 @@ var onUserUpdate = (options) => async (user, context) => {
89
98
  var onUserDelete = (options) => async (user, context) => {
90
99
  if (context && options.createCustomerOnSignUp) {
91
100
  try {
101
+ if (user.isAnonymous) {
102
+ return;
103
+ }
92
104
  if (user.email) {
93
105
  const { result: existingCustomers } = await options.client.customers.list({ email: user.email });
94
106
  const existingCustomer = existingCustomers.items[0];
@@ -117,13 +129,16 @@ import { APIError as APIError2 } from "better-auth/api";
117
129
  import { sessionMiddleware } from "better-auth/api";
118
130
  import { createAuthEndpoint } from "better-auth/plugins";
119
131
  import { z } from "zod";
120
- var portal = ({ returnUrl } = {}) => (polar2) => {
132
+ var portal = ({ returnUrl, theme } = {}) => (polar2) => {
121
133
  const retUrl = returnUrl ? new URL(returnUrl) : void 0;
122
134
  return {
123
135
  portal: createAuthEndpoint(
124
136
  "/customer/portal",
125
137
  {
126
- method: "GET",
138
+ method: ["GET", "POST"],
139
+ body: z.object({
140
+ redirect: z.boolean().optional()
141
+ }).optional(),
127
142
  use: [sessionMiddleware]
128
143
  },
129
144
  async (ctx) => {
@@ -132,14 +147,23 @@ var portal = ({ returnUrl } = {}) => (polar2) => {
132
147
  message: "User not found"
133
148
  });
134
149
  }
150
+ if (ctx.context.session?.user["isAnonymous"]) {
151
+ throw new APIError2("UNAUTHORIZED", {
152
+ message: "Anonymous users cannot access the portal"
153
+ });
154
+ }
135
155
  try {
136
156
  const customerSession = await polar2.customerSessions.create({
137
157
  externalCustomerId: ctx.context.session?.user.id,
138
158
  returnUrl: retUrl ? decodeURI(retUrl.toString()) : void 0
139
159
  });
160
+ const portalUrl = new URL(customerSession.customerPortalUrl);
161
+ if (theme) {
162
+ portalUrl.searchParams.set("theme", theme);
163
+ }
140
164
  return ctx.json({
141
- url: customerSession.customerPortalUrl,
142
- redirect: true
165
+ url: portalUrl.toString(),
166
+ redirect: ctx.body?.redirect ?? true
143
167
  });
144
168
  } catch (e) {
145
169
  if (e instanceof Error) {
@@ -342,11 +366,24 @@ var CheckoutParams = z2.object({
342
366
  slug: z2.string().optional(),
343
367
  referenceId: z2.string().optional(),
344
368
  customFieldData: z2.record(z2.string(), z2.union([z2.string(), z2.number(), z2.boolean()])).optional(),
345
- metadata: z2.record(z2.string(), z2.union([z2.string(), z2.number(), z2.boolean()])).optional(),
369
+ metadata: z2.record(z2.string(), z2.union([z2.string().max(500), z2.number(), z2.boolean()])).refine((obj) => Object.keys(obj).length <= 50, {
370
+ message: "Metadata can have at most 50 key-value pairs"
371
+ }).refine((obj) => Object.keys(obj).every((key) => key.length <= 40), {
372
+ message: "Metadata keys must be at most 40 characters"
373
+ }).optional(),
346
374
  allowDiscountCodes: z2.coerce.boolean().optional(),
347
375
  discountId: z2.string().optional(),
348
376
  redirect: z2.coerce.boolean().optional(),
349
- embedOrigin: z2.string().url().optional()
377
+ embedOrigin: z2.string().url().optional(),
378
+ successUrl: z2.string().refine((val) => val.startsWith("/") || URL.canParse(val), {
379
+ message: "Must be a valid URL or a relative path starting with /"
380
+ }).optional(),
381
+ returnUrl: z2.string().refine((val) => val.startsWith("/") || URL.canParse(val), {
382
+ message: "Must be a valid URL or a relative path starting with /"
383
+ }).optional(),
384
+ allowTrial: z2.boolean().optional(),
385
+ trialInterval: z2.enum(["day", "week", "month", "year"]).optional(),
386
+ trialIntervalCount: z2.number().int().min(1).max(1e3).optional()
350
387
  });
351
388
  var checkout = (checkoutOptions = {}) => (polar2) => {
352
389
  return {
@@ -373,17 +410,26 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
373
410
  } else {
374
411
  productIds = Array.isArray(ctx.body.products) ? ctx.body.products.filter((id) => id !== void 0) : [ctx.body.products].filter((id) => id !== void 0);
375
412
  }
376
- if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {
377
- throw new APIError3("UNAUTHORIZED", {
378
- message: "You must be logged in to checkout"
379
- });
413
+ if (checkoutOptions.authenticatedUsersOnly) {
414
+ if (!session?.user.id) {
415
+ throw new APIError3("UNAUTHORIZED", {
416
+ message: "You must be logged in to checkout"
417
+ });
418
+ }
419
+ if (session.user["isAnonymous"]) {
420
+ throw new APIError3("UNAUTHORIZED", {
421
+ message: "Anonymous users are not allowed to checkout"
422
+ });
423
+ }
380
424
  }
425
+ const successUrl = ctx.body.successUrl ?? checkoutOptions.successUrl;
426
+ const returnUrl = ctx.body.returnUrl ?? checkoutOptions.returnUrl;
381
427
  try {
382
428
  const checkout2 = await polar2.checkouts.create({
383
429
  externalCustomerId: session?.user.id,
384
430
  products: productIds,
385
- successUrl: checkoutOptions.successUrl ? new URL(
386
- checkoutOptions.successUrl,
431
+ successUrl: successUrl ? new URL(
432
+ successUrl,
387
433
  ctx.request?.url ?? ctx.context.baseURL
388
434
  ).toString() : void 0,
389
435
  metadata: ctx.body.referenceId ? {
@@ -394,8 +440,11 @@ var checkout = (checkoutOptions = {}) => (polar2) => {
394
440
  allowDiscountCodes: ctx.body.allowDiscountCodes ?? true,
395
441
  discountId: ctx.body.discountId,
396
442
  embedOrigin: ctx.body.embedOrigin,
397
- returnUrl: checkoutOptions.returnUrl ? new URL(
398
- checkoutOptions.returnUrl,
443
+ allowTrial: ctx.body.allowTrial,
444
+ trialInterval: ctx.body.trialInterval,
445
+ trialIntervalCount: ctx.body.trialIntervalCount,
446
+ returnUrl: returnUrl ? new URL(
447
+ returnUrl,
399
448
  ctx.request?.url ?? ctx.context.baseURL
400
449
  ).toString() : void 0
401
450
  });