@pradip1995/segment-login-template 0.5.26 → 0.5.28

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 (43) hide show
  1. package/package.json +58 -54
  2. package/src/account-addresses.tsx +307 -287
  3. package/src/account-guest-orders.tsx +98 -98
  4. package/src/account-orders.tsx +260 -260
  5. package/src/account-overview.tsx +133 -133
  6. package/src/account-payment-methods.tsx +328 -328
  7. package/src/account-profile-logic.ts +282 -282
  8. package/src/account-profile.tsx +111 -111
  9. package/src/account-theme.ts +91 -91
  10. package/src/account-utils.ts +20 -20
  11. package/src/address-actions.ts +74 -0
  12. package/src/auth-server.ts +442 -442
  13. package/src/card-expiry.test.ts +71 -0
  14. package/src/card-expiry.ts +29 -0
  15. package/src/commerce-auth-otp-modal.css +125 -125
  16. package/src/commerce-auth-otp-modal.tsx +121 -121
  17. package/src/default-address.test.ts +143 -0
  18. package/src/default-address.ts +51 -0
  19. package/src/forgot-password-form.tsx +109 -109
  20. package/src/google-auth-section.tsx +22 -22
  21. package/src/google-sign-in-section.tsx +67 -67
  22. package/src/guest-order-modal.tsx +215 -215
  23. package/src/index.ts +7 -7
  24. package/src/is-next-redirect.ts +60 -60
  25. package/src/login-brand-panel.tsx +159 -158
  26. package/src/login-form.tsx +469 -469
  27. package/src/manifest.ts +11 -11
  28. package/src/order-history-status.test.ts +224 -224
  29. package/src/order-history-status.ts +244 -244
  30. package/src/otp-component.ts +14 -14
  31. package/src/otp-input.tsx +46 -46
  32. package/src/otp-unified-form.tsx +333 -333
  33. package/src/otp-verification-modal.tsx +112 -112
  34. package/src/payment-methods-actions.ts +243 -243
  35. package/src/profile-email-field.tsx +116 -116
  36. package/src/profile-email.test.ts +182 -182
  37. package/src/profile-email.ts +54 -54
  38. package/src/profile-phone-field.tsx +119 -119
  39. package/src/register-form.tsx +339 -339
  40. package/src/reorder-checkout-address.ts +103 -101
  41. package/src/reset-password-form.tsx +103 -103
  42. package/src/reset-password-page.tsx +12 -12
  43. package/src/segment.tsx +272 -272
@@ -1,243 +1,243 @@
1
- "use server"
2
-
3
- import { getAuthHeaders } from "@pradip1995/commerce-core/data/cookies"
4
-
5
- type PaymentMethodType = "upi" | "bank" | "card"
6
-
7
- type ActionResult =
8
- | { success: true }
9
- | { success: false; error: string; field_errors?: Record<string, string> }
10
-
11
- function getBaseUrl() {
12
- return process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
13
- }
14
-
15
- function getPublishableKey() {
16
- return process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || ""
17
- }
18
-
19
- function trimAll(detail_json: Record<string, string>): Record<string, string> {
20
- const out: Record<string, string> = {}
21
- for (const [key, value] of Object.entries(detail_json)) {
22
- if (typeof value !== "string") continue
23
- const trimmed = value.trim()
24
- if (!trimmed) continue
25
- out[key] = key === "ifsc" ? trimmed.toUpperCase() : trimmed
26
- }
27
- return out
28
- }
29
-
30
- function validatePaymentDetail(
31
- type: PaymentMethodType,
32
- detail_json: Record<string, string>
33
- ): Record<string, string> {
34
- const field_errors: Record<string, string> = {}
35
-
36
- if (type === "upi") {
37
- const upi = detail_json.upi_id || ""
38
- if (!upi) {
39
- field_errors.upi_id = "UPI ID is required"
40
- } else if (!/^[\w.\-]{2,256}@[a-zA-Z]{2,64}$/.test(upi)) {
41
- field_errors.upi_id = "Enter a valid UPI ID (e.g. name@upi)"
42
- }
43
- }
44
-
45
- if (type === "bank") {
46
- const holder = detail_json.account_holder_name || ""
47
- const bank = detail_json.bank_name || ""
48
- const account = detail_json.account_number || ""
49
- const ifsc = detail_json.ifsc || ""
50
-
51
- if (!holder) {
52
- field_errors.account_holder_name = "Account holder name is required"
53
- }
54
- if (!bank) {
55
- field_errors.bank_name = "Bank name is required"
56
- }
57
- if (!account) {
58
- field_errors.account_number = "Account number is required"
59
- } else if (!/^\d{9,18}$/.test(account)) {
60
- field_errors.account_number = "Account number must be 9–18 digits"
61
- }
62
- if (!ifsc) {
63
- field_errors.ifsc = "IFSC is required"
64
- } else if (!/^[A-Z]{4}0[A-Z0-9]{6}$/.test(ifsc)) {
65
- field_errors.ifsc =
66
- "Enter a valid IFSC (e.g. HDFC0001234) — 4 letters, 0, then 6 characters"
67
- }
68
- }
69
-
70
- if (type === "card") {
71
- const holder = detail_json.card_holder_name || ""
72
- const number = detail_json.card_number || ""
73
- const expiry = detail_json.expiry_date || ""
74
- const cvv = detail_json.cvv || ""
75
-
76
- if (!holder) {
77
- field_errors.card_holder_name = "Card holder name is required"
78
- }
79
- if (!number) {
80
- field_errors.card_number = "Card number is required"
81
- } else if (!/^\d{13,19}$/.test(number.replace(/\s+/g, ""))) {
82
- field_errors.card_number = "Enter a valid card number (13–19 digits)"
83
- }
84
- if (!expiry) {
85
- field_errors.expiry_date = "Expiry date is required"
86
- } else if (!/^(0[1-9]|1[0-2])\/\d{2}$/.test(expiry)) {
87
- field_errors.expiry_date = "Use MM/YY format (e.g. 08/28)"
88
- }
89
- if (!cvv) {
90
- field_errors.cvv = "CVV is required"
91
- } else if (!/^\d{3,4}$/.test(cvv)) {
92
- field_errors.cvv = "CVV must be 3 or 4 digits"
93
- }
94
- }
95
-
96
- return field_errors
97
- }
98
-
99
- function firstFieldError(field_errors: Record<string, string>): string {
100
- return Object.values(field_errors)[0] || "Invalid payment details"
101
- }
102
-
103
- function parseBackendError(body: unknown, status: number): string {
104
- if (!body || typeof body !== "object") {
105
- return status === 400
106
- ? "Invalid payment details. Check the entered values."
107
- : `Request failed (${status})`
108
- }
109
-
110
- const data = body as {
111
- message?: unknown
112
- error?: unknown
113
- errors?: Array<{ message?: string; path?: string[]; field?: string }>
114
- }
115
-
116
- if (Array.isArray(data.errors) && data.errors.length > 0) {
117
- const details = data.errors
118
- .map((item) => {
119
- const path = item.path?.join(".") || item.field
120
- const msg = item.message?.trim()
121
- if (path && msg) return `${path}: ${msg}`
122
- return msg
123
- })
124
- .filter((msg): msg is string => Boolean(msg))
125
- if (details.length) return details.join("; ")
126
- }
127
-
128
- const primary =
129
- typeof data.message === "string" && data.message.trim()
130
- ? data.message.trim()
131
- : typeof data.error === "string" && data.error.trim()
132
- ? data.error.trim()
133
- : ""
134
-
135
- if (primary) {
136
- if (/^invalid request( data)?\.?$/i.test(primary)) {
137
- return "Invalid payment details. Check required fields and try again."
138
- }
139
- if (/^failed to set default payment detail\.?$/i.test(primary)) {
140
- const detail =
141
- typeof data.error === "string" && data.error.trim() ? data.error.trim() : ""
142
- if (detail) {
143
- if (/already exists|unique|duplicate/i.test(detail)) {
144
- return "Could not update your default payment method. Please try again."
145
- }
146
- return detail
147
- }
148
- return "Could not update your default payment method. Please try again."
149
- }
150
- if (/payment details? with customer(_|\s)?id:?\s*.*already exists/i.test(primary)) {
151
- return "Could not update your default payment method. Please try again."
152
- }
153
- if (/already exists/i.test(primary) && /customer/i.test(primary)) {
154
- return "Could not update your default payment method. Please try again."
155
- }
156
- if (/^failed to delete payment detail\.?$/i.test(primary)) {
157
- const detail =
158
- typeof data.error === "string" && data.error.trim() ? data.error.trim() : ""
159
- if (detail) return detail
160
- }
161
- return primary
162
- }
163
-
164
- return `Request failed (${status})`
165
- }
166
-
167
- async function paymentDetailsFetch(
168
- path: string,
169
- init: { method: string; body?: Record<string, unknown> }
170
- ): Promise<ActionResult> {
171
- const authHeaders = await getAuthHeaders()
172
- if (!("authorization" in authHeaders)) {
173
- return { success: false, error: "Please sign in to manage payment methods" }
174
- }
175
-
176
- const response = await fetch(`${getBaseUrl()}${path}`, {
177
- method: init.method,
178
- headers: {
179
- Accept: "application/json",
180
- "Content-Type": "application/json",
181
- "x-publishable-api-key": getPublishableKey(),
182
- ...authHeaders,
183
- },
184
- body: init.body ? JSON.stringify(init.body) : undefined,
185
- cache: "no-store",
186
- })
187
-
188
- if (response.ok) {
189
- return { success: true }
190
- }
191
-
192
- const body = await response.json().catch(() => null)
193
- return {
194
- success: false,
195
- error: parseBackendError(body, response.status),
196
- }
197
- }
198
-
199
- export async function addPaymentMethodAction(
200
- type: PaymentMethodType,
201
- detail_json: Record<string, string>
202
- ): Promise<ActionResult> {
203
- const normalized = trimAll(detail_json)
204
- if (normalized.card_number) {
205
- normalized.card_number = normalized.card_number.replace(/\s+/g, "")
206
- }
207
-
208
- const field_errors = validatePaymentDetail(type, normalized)
209
- if (Object.keys(field_errors).length > 0) {
210
- return {
211
- success: false,
212
- error: firstFieldError(field_errors),
213
- field_errors,
214
- }
215
- }
216
-
217
- return paymentDetailsFetch("/store/payment-details", {
218
- method: "POST",
219
- body: { type, detail_json: normalized },
220
- })
221
- }
222
-
223
- export async function makeDefaultPaymentMethodAction(
224
- id: string
225
- ): Promise<ActionResult> {
226
- if (!id) {
227
- return { success: false, error: "Payment method id is required" }
228
- }
229
- return paymentDetailsFetch(`/store/payment-details/${id}/make-default`, {
230
- method: "POST",
231
- })
232
- }
233
-
234
- export async function deletePaymentMethodAction(
235
- id: string
236
- ): Promise<ActionResult> {
237
- if (!id) {
238
- return { success: false, error: "Payment method id is required" }
239
- }
240
- return paymentDetailsFetch(`/store/payment-details/${id}`, {
241
- method: "DELETE",
242
- })
243
- }
1
+ "use server"
2
+
3
+ import { getAuthHeaders } from "@pradip1995/commerce-core/data/cookies"
4
+ import { getCardExpiryError } from "./card-expiry"
5
+
6
+ type PaymentMethodType = "upi" | "bank" | "card"
7
+
8
+ type ActionResult =
9
+ | { success: true }
10
+ | { success: false; error: string; field_errors?: Record<string, string> }
11
+
12
+ function getBaseUrl() {
13
+ return process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
14
+ }
15
+
16
+ function getPublishableKey() {
17
+ return process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || ""
18
+ }
19
+
20
+ function trimAll(detail_json: Record<string, string>): Record<string, string> {
21
+ const out: Record<string, string> = {}
22
+ for (const [key, value] of Object.entries(detail_json)) {
23
+ if (typeof value !== "string") continue
24
+ const trimmed = value.trim()
25
+ if (!trimmed) continue
26
+ out[key] = key === "ifsc" ? trimmed.toUpperCase() : trimmed
27
+ }
28
+ return out
29
+ }
30
+
31
+ function validatePaymentDetail(
32
+ type: PaymentMethodType,
33
+ detail_json: Record<string, string>
34
+ ): Record<string, string> {
35
+ const field_errors: Record<string, string> = {}
36
+
37
+ if (type === "upi") {
38
+ const upi = detail_json.upi_id || ""
39
+ if (!upi) {
40
+ field_errors.upi_id = "UPI ID is required"
41
+ } else if (!/^[\w.\-]{2,256}@[a-zA-Z]{2,64}$/.test(upi)) {
42
+ field_errors.upi_id = "Enter a valid UPI ID (e.g. name@upi)"
43
+ }
44
+ }
45
+
46
+ if (type === "bank") {
47
+ const holder = detail_json.account_holder_name || ""
48
+ const bank = detail_json.bank_name || ""
49
+ const account = detail_json.account_number || ""
50
+ const ifsc = detail_json.ifsc || ""
51
+
52
+ if (!holder) {
53
+ field_errors.account_holder_name = "Account holder name is required"
54
+ }
55
+ if (!bank) {
56
+ field_errors.bank_name = "Bank name is required"
57
+ }
58
+ if (!account) {
59
+ field_errors.account_number = "Account number is required"
60
+ } else if (!/^\d{9,18}$/.test(account)) {
61
+ field_errors.account_number = "Account number must be 9–18 digits"
62
+ }
63
+ if (!ifsc) {
64
+ field_errors.ifsc = "IFSC is required"
65
+ } else if (!/^[A-Z]{4}0[A-Z0-9]{6}$/.test(ifsc)) {
66
+ field_errors.ifsc =
67
+ "Enter a valid IFSC (e.g. HDFC0001234) — 4 letters, 0, then 6 characters"
68
+ }
69
+ }
70
+
71
+ if (type === "card") {
72
+ const holder = detail_json.card_holder_name || ""
73
+ const number = detail_json.card_number || ""
74
+ const expiry = detail_json.expiry_date || ""
75
+ const cvv = detail_json.cvv || ""
76
+
77
+ if (!holder) {
78
+ field_errors.card_holder_name = "Card holder name is required"
79
+ }
80
+ if (!number) {
81
+ field_errors.card_number = "Card number is required"
82
+ } else if (!/^\d{13,19}$/.test(number.replace(/\s+/g, ""))) {
83
+ field_errors.card_number = "Enter a valid card number (13–19 digits)"
84
+ }
85
+ const expiryError = getCardExpiryError(expiry)
86
+ if (expiryError) {
87
+ field_errors.expiry_date = expiryError
88
+ }
89
+ if (!cvv) {
90
+ field_errors.cvv = "CVV is required"
91
+ } else if (!/^\d{3,4}$/.test(cvv)) {
92
+ field_errors.cvv = "CVV must be 3 or 4 digits"
93
+ }
94
+ }
95
+
96
+ return field_errors
97
+ }
98
+
99
+ function firstFieldError(field_errors: Record<string, string>): string {
100
+ return Object.values(field_errors)[0] || "Invalid payment details"
101
+ }
102
+
103
+ function parseBackendError(body: unknown, status: number): string {
104
+ if (!body || typeof body !== "object") {
105
+ return status === 400
106
+ ? "Invalid payment details. Check the entered values."
107
+ : `Request failed (${status})`
108
+ }
109
+
110
+ const data = body as {
111
+ message?: unknown
112
+ error?: unknown
113
+ errors?: Array<{ message?: string; path?: string[]; field?: string }>
114
+ }
115
+
116
+ if (Array.isArray(data.errors) && data.errors.length > 0) {
117
+ const details = data.errors
118
+ .map((item) => {
119
+ const path = item.path?.join(".") || item.field
120
+ const msg = item.message?.trim()
121
+ if (path && msg) return `${path}: ${msg}`
122
+ return msg
123
+ })
124
+ .filter((msg): msg is string => Boolean(msg))
125
+ if (details.length) return details.join("; ")
126
+ }
127
+
128
+ const primary =
129
+ typeof data.message === "string" && data.message.trim()
130
+ ? data.message.trim()
131
+ : typeof data.error === "string" && data.error.trim()
132
+ ? data.error.trim()
133
+ : ""
134
+
135
+ if (primary) {
136
+ if (/^invalid request( data)?\.?$/i.test(primary)) {
137
+ return "Invalid payment details. Check required fields and try again."
138
+ }
139
+ if (/^failed to set default payment detail\.?$/i.test(primary)) {
140
+ const detail =
141
+ typeof data.error === "string" && data.error.trim() ? data.error.trim() : ""
142
+ if (detail) {
143
+ if (/already exists|unique|duplicate/i.test(detail)) {
144
+ return "Could not update your default payment method. Please try again."
145
+ }
146
+ return detail
147
+ }
148
+ return "Could not update your default payment method. Please try again."
149
+ }
150
+ if (/payment details? with customer(_|\s)?id:?\s*.*already exists/i.test(primary)) {
151
+ return "Could not update your default payment method. Please try again."
152
+ }
153
+ if (/already exists/i.test(primary) && /customer/i.test(primary)) {
154
+ return "Could not update your default payment method. Please try again."
155
+ }
156
+ if (/^failed to delete payment detail\.?$/i.test(primary)) {
157
+ const detail =
158
+ typeof data.error === "string" && data.error.trim() ? data.error.trim() : ""
159
+ if (detail) return detail
160
+ }
161
+ return primary
162
+ }
163
+
164
+ return `Request failed (${status})`
165
+ }
166
+
167
+ async function paymentDetailsFetch(
168
+ path: string,
169
+ init: { method: string; body?: Record<string, unknown> }
170
+ ): Promise<ActionResult> {
171
+ const authHeaders = await getAuthHeaders()
172
+ if (!("authorization" in authHeaders)) {
173
+ return { success: false, error: "Please sign in to manage payment methods" }
174
+ }
175
+
176
+ const response = await fetch(`${getBaseUrl()}${path}`, {
177
+ method: init.method,
178
+ headers: {
179
+ Accept: "application/json",
180
+ "Content-Type": "application/json",
181
+ "x-publishable-api-key": getPublishableKey(),
182
+ ...authHeaders,
183
+ },
184
+ body: init.body ? JSON.stringify(init.body) : undefined,
185
+ cache: "no-store",
186
+ })
187
+
188
+ if (response.ok) {
189
+ return { success: true }
190
+ }
191
+
192
+ const body = await response.json().catch(() => null)
193
+ return {
194
+ success: false,
195
+ error: parseBackendError(body, response.status),
196
+ }
197
+ }
198
+
199
+ export async function addPaymentMethodAction(
200
+ type: PaymentMethodType,
201
+ detail_json: Record<string, string>
202
+ ): Promise<ActionResult> {
203
+ const normalized = trimAll(detail_json)
204
+ if (normalized.card_number) {
205
+ normalized.card_number = normalized.card_number.replace(/\s+/g, "")
206
+ }
207
+
208
+ const field_errors = validatePaymentDetail(type, normalized)
209
+ if (Object.keys(field_errors).length > 0) {
210
+ return {
211
+ success: false,
212
+ error: firstFieldError(field_errors),
213
+ field_errors,
214
+ }
215
+ }
216
+
217
+ return paymentDetailsFetch("/store/payment-details", {
218
+ method: "POST",
219
+ body: { type, detail_json: normalized },
220
+ })
221
+ }
222
+
223
+ export async function makeDefaultPaymentMethodAction(
224
+ id: string
225
+ ): Promise<ActionResult> {
226
+ if (!id) {
227
+ return { success: false, error: "Payment method id is required" }
228
+ }
229
+ return paymentDetailsFetch(`/store/payment-details/${id}/make-default`, {
230
+ method: "POST",
231
+ })
232
+ }
233
+
234
+ export async function deletePaymentMethodAction(
235
+ id: string
236
+ ): Promise<ActionResult> {
237
+ if (!id) {
238
+ return { success: false, error: "Payment method id is required" }
239
+ }
240
+ return paymentDetailsFetch(`/store/payment-details/${id}`, {
241
+ method: "DELETE",
242
+ })
243
+ }