@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,282 +1,282 @@
1
- "use client"
2
-
3
- import { useActionState, useState } from "react"
4
- import type { HttpTypes } from "@medusajs/types"
5
- import { updateCustomer } from "@pradip1995/commerce-core/data/customer"
6
- import {
7
- combinePhoneParts,
8
- isValidPhoneParts,
9
- parsePhoneParts,
10
- } from "@pradip1995/commerce-core/util/phone"
11
- import {
12
- requestContactChange,
13
- sendRegistrationOtp,
14
- verifyContactChange,
15
- verifyRegistrationOtp,
16
- } from "./auth-server"
17
- import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
18
- import {
19
- EMAIL_OTP_ERRORS,
20
- isValidEmail,
21
- normalizeEmail,
22
- resolveEmailOtpFlow,
23
- type EmailOtpFlow,
24
- } from "./profile-email"
25
-
26
- export function customerPhoneToInputValue(phone?: string | null): string {
27
- if (!phone?.trim()) return ""
28
- const parts = parsePhoneParts(phone)
29
- return combinePhoneParts(parts.dialCode, parts.localNumber)
30
- }
31
-
32
- export function phoneDigits(value: string): string {
33
- return value.replace(/\D/g, "")
34
- }
35
-
36
- export function phoneToE164(value: string): string {
37
- const parts = parsePhoneParts(value)
38
- const local = parts.localNumber.replace(/\D/g, "")
39
- const dial = parts.dialCode.replace(/\D/g, "")
40
- if (!local) return ""
41
- return `+${dial}${local}`
42
- }
43
-
44
- function readVerified(
45
- customer: HttpTypes.StoreCustomer,
46
- field: "email_verified" | "phone_verified"
47
- ): boolean {
48
- const direct = (customer as Record<string, unknown>)[field]
49
- if (direct === true || direct === "true") return true
50
- const meta = customer.metadata as Record<string, unknown> | undefined
51
- const fromMeta = meta?.[field]
52
- return fromMeta === true || fromMeta === "true"
53
- }
54
-
55
- export function useAccountProfileLogic(
56
- customer: HttpTypes.StoreCustomer,
57
- countryCode: string
58
- ) {
59
- const [message, setMessage] = useState<string | null>(null)
60
- const [phone, setPhone] = useState(() => customerPhoneToInputValue(customer.phone))
61
- const [phoneError, setPhoneError] = useState<string | null>(null)
62
- const [email, setEmail] = useState(() => customer.email || "")
63
- const [emailError, setEmailError] = useState<string | null>(null)
64
- const [otpOpen, setOtpOpen] = useState(false)
65
- const [otp, setOtp] = useState("")
66
- const [otpToken, setOtpToken] = useState<string | null>(null)
67
- const [otpType, setOtpType] = useState<"email_verification" | "phone_verification">(
68
- "email_verification"
69
- )
70
- const [otpFlow, setOtpFlow] = useState<EmailOtpFlow>("verify_existing")
71
- const [otpError, setOtpError] = useState<string | null>(null)
72
- const [otpPending, setOtpPending] = useState(false)
73
- const [phoneVerified, setPhoneVerified] = useState(() =>
74
- readVerified(customer, "phone_verified")
75
- )
76
- const [emailVerified, setEmailVerified] = useState(() =>
77
- readVerified(customer, "email_verified")
78
- )
79
-
80
- const phoneParts = parsePhoneParts(phone)
81
- const phoneReady = isValidPhoneParts(phoneParts.dialCode, phoneParts.localNumber)
82
- const emailReady = isValidEmail(email)
83
-
84
- async function saveProfile(_prev: unknown, formData: FormData) {
85
- try {
86
- const fullName = String(formData.get("full_name") || "").trim()
87
- const [first_name, ...rest] = fullName.split(/\s+/)
88
- const e164 = phoneToE164(phone)
89
- await updateCustomer({
90
- first_name: first_name || "",
91
- last_name: rest.join(" ") || ".",
92
- phone: e164 || undefined,
93
- })
94
- if (e164 && phoneDigits(e164) !== phoneDigits(customer.phone || "")) {
95
- setPhoneVerified(false)
96
- }
97
- return { ok: true as const, error: null as string | null }
98
- } catch (err) {
99
- if (isNextRedirect(err)) throw err
100
- return {
101
- ok: false as const,
102
- error: safeErrorMessage(err, "Failed to update profile"),
103
- }
104
- }
105
- }
106
-
107
- const [state, formAction, pending] = useActionState(saveProfile, {
108
- ok: false as const,
109
- error: null as string | null,
110
- })
111
-
112
- async function ensurePhoneSavedForVerification(): Promise<boolean> {
113
- setPhoneError(null)
114
- if (!phoneReady) {
115
- setPhoneError("Please enter a valid mobile number before verifying.")
116
- return false
117
- }
118
-
119
- const e164 = phoneToE164(phone)
120
- if (phoneDigits(e164) === phoneDigits(customer.phone || "")) {
121
- return true
122
- }
123
-
124
- try {
125
- await updateCustomer({ phone: e164 })
126
- return true
127
- } catch (err) {
128
- if (isNextRedirect(err)) throw err
129
- setPhoneError(
130
- safeErrorMessage(err, "Failed to save mobile number. Please try again.")
131
- )
132
- return false
133
- }
134
- }
135
-
136
- async function sendVerifyOtp(type: "email_verification" | "phone_verification") {
137
- setOtpType(type)
138
- setOtpPending(true)
139
- setOtpError(null)
140
-
141
- try {
142
- if (type === "phone_verification") {
143
- const saved = await ensurePhoneSavedForVerification()
144
- if (!saved) return
145
- setOtpFlow("verify_existing")
146
- }
147
-
148
- if (type === "email_verification") {
149
- const resolved = resolveEmailOtpFlow({
150
- currentEmail: customer.email,
151
- draftEmail: email,
152
- })
153
- if (!resolved.ok) {
154
- setEmailError(EMAIL_OTP_ERRORS[resolved.reason])
155
- return
156
- }
157
- setOtpFlow(resolved.flow)
158
- const res =
159
- resolved.flow === "change_contact"
160
- ? await requestContactChange({ email: resolved.email })
161
- : await sendRegistrationOtp(customer.id, type)
162
- if (!res?.success) {
163
- throw new Error(res?.error || "Failed to send code")
164
- }
165
- setOtpToken(res.token ?? null)
166
- setOtp("")
167
- setOtpOpen(true)
168
- return
169
- }
170
-
171
- const res = await sendRegistrationOtp(customer.id, type)
172
- if (!res?.success) {
173
- throw new Error(res?.error || "Failed to send code")
174
- }
175
- setOtpToken(res.token ?? null)
176
- setOtp("")
177
- setOtpOpen(true)
178
- } catch (err) {
179
- if (isNextRedirect(err)) throw err
180
- const msg = safeErrorMessage(err, "Failed to send code")
181
- setOtpError(msg)
182
- if (type === "phone_verification") {
183
- setPhoneError(msg)
184
- }
185
- if (type === "email_verification") {
186
- setEmailError(msg)
187
- }
188
- } finally {
189
- setOtpPending(false)
190
- }
191
- }
192
-
193
- async function verifyOtp() {
194
- if (otp.length < 6) return
195
- setOtpPending(true)
196
- setOtpError(null)
197
- try {
198
- if (otpType === "email_verification" && otpFlow === "change_contact") {
199
- const res = await verifyContactChange({
200
- otpToken: otpToken || "",
201
- code: otp,
202
- })
203
- if (!res?.success) {
204
- throw new Error(res?.error || "Invalid code")
205
- }
206
- setEmailVerified(true)
207
- } else {
208
- const res = await verifyRegistrationOtp({
209
- otpToken: otpToken || "",
210
- code: otp,
211
- countryCode,
212
- skipRedirect: true,
213
- })
214
- if (!res?.success) {
215
- throw new Error(res?.error || "Invalid code")
216
- }
217
- if (otpType === "phone_verification") {
218
- setPhoneVerified(true)
219
- }
220
- if (otpType === "email_verification") {
221
- setEmailVerified(true)
222
- }
223
- }
224
- setOtpOpen(false)
225
- setMessage("Verification successful.")
226
- window.location.reload()
227
- } catch (err) {
228
- if (isNextRedirect(err)) throw err
229
- setOtpError(safeErrorMessage(err, "Verification failed"))
230
- setOtpPending(false)
231
- }
232
- }
233
-
234
- function handlePhoneChange(value: string) {
235
- setPhone(value)
236
- setPhoneError(null)
237
- const nextE164 = phoneToE164(value)
238
- if (
239
- nextE164 &&
240
- phoneDigits(nextE164) !== phoneDigits(customer.phone || "")
241
- ) {
242
- setPhoneVerified(false)
243
- }
244
- }
245
-
246
- function handleEmailChange(value: string) {
247
- setEmail(value)
248
- setEmailError(null)
249
- if (normalizeEmail(value) !== normalizeEmail(customer.email)) {
250
- setEmailVerified(false)
251
- }
252
- }
253
-
254
- const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
255
-
256
- return {
257
- fullName,
258
- phone,
259
- handlePhoneChange,
260
- phoneReady,
261
- phoneError,
262
- phoneVerified,
263
- email,
264
- handleEmailChange,
265
- emailReady,
266
- emailError,
267
- emailVerified,
268
- message,
269
- state,
270
- formAction,
271
- pending,
272
- otpOpen,
273
- setOtpOpen,
274
- otp,
275
- setOtp,
276
- otpType,
277
- otpError,
278
- otpPending,
279
- sendVerifyOtp,
280
- verifyOtp,
281
- }
282
- }
1
+ "use client"
2
+
3
+ import { useActionState, useState } from "react"
4
+ import type { HttpTypes } from "@medusajs/types"
5
+ import { updateCustomer } from "@pradip1995/commerce-core/data/customer"
6
+ import {
7
+ combinePhoneParts,
8
+ isValidPhoneParts,
9
+ parsePhoneParts,
10
+ } from "@pradip1995/commerce-core/util/phone"
11
+ import {
12
+ requestContactChange,
13
+ sendRegistrationOtp,
14
+ verifyContactChange,
15
+ verifyRegistrationOtp,
16
+ } from "./auth-server"
17
+ import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
18
+ import {
19
+ EMAIL_OTP_ERRORS,
20
+ isValidEmail,
21
+ normalizeEmail,
22
+ resolveEmailOtpFlow,
23
+ type EmailOtpFlow,
24
+ } from "./profile-email"
25
+
26
+ export function customerPhoneToInputValue(phone?: string | null): string {
27
+ if (!phone?.trim()) return ""
28
+ const parts = parsePhoneParts(phone)
29
+ return combinePhoneParts(parts.dialCode, parts.localNumber)
30
+ }
31
+
32
+ export function phoneDigits(value: string): string {
33
+ return value.replace(/\D/g, "")
34
+ }
35
+
36
+ export function phoneToE164(value: string): string {
37
+ const parts = parsePhoneParts(value)
38
+ const local = parts.localNumber.replace(/\D/g, "")
39
+ const dial = parts.dialCode.replace(/\D/g, "")
40
+ if (!local) return ""
41
+ return `+${dial}${local}`
42
+ }
43
+
44
+ function readVerified(
45
+ customer: HttpTypes.StoreCustomer,
46
+ field: "email_verified" | "phone_verified"
47
+ ): boolean {
48
+ const direct = (customer as Record<string, unknown>)[field]
49
+ if (direct === true || direct === "true") return true
50
+ const meta = customer.metadata as Record<string, unknown> | undefined
51
+ const fromMeta = meta?.[field]
52
+ return fromMeta === true || fromMeta === "true"
53
+ }
54
+
55
+ export function useAccountProfileLogic(
56
+ customer: HttpTypes.StoreCustomer,
57
+ countryCode: string
58
+ ) {
59
+ const [message, setMessage] = useState<string | null>(null)
60
+ const [phone, setPhone] = useState(() => customerPhoneToInputValue(customer.phone))
61
+ const [phoneError, setPhoneError] = useState<string | null>(null)
62
+ const [email, setEmail] = useState(() => customer.email || "")
63
+ const [emailError, setEmailError] = useState<string | null>(null)
64
+ const [otpOpen, setOtpOpen] = useState(false)
65
+ const [otp, setOtp] = useState("")
66
+ const [otpToken, setOtpToken] = useState<string | null>(null)
67
+ const [otpType, setOtpType] = useState<"email_verification" | "phone_verification">(
68
+ "email_verification"
69
+ )
70
+ const [otpFlow, setOtpFlow] = useState<EmailOtpFlow>("verify_existing")
71
+ const [otpError, setOtpError] = useState<string | null>(null)
72
+ const [otpPending, setOtpPending] = useState(false)
73
+ const [phoneVerified, setPhoneVerified] = useState(() =>
74
+ readVerified(customer, "phone_verified")
75
+ )
76
+ const [emailVerified, setEmailVerified] = useState(() =>
77
+ readVerified(customer, "email_verified")
78
+ )
79
+
80
+ const phoneParts = parsePhoneParts(phone)
81
+ const phoneReady = isValidPhoneParts(phoneParts.dialCode, phoneParts.localNumber)
82
+ const emailReady = isValidEmail(email)
83
+
84
+ async function saveProfile(_prev: unknown, formData: FormData) {
85
+ try {
86
+ const fullName = String(formData.get("full_name") || "").trim()
87
+ const [first_name, ...rest] = fullName.split(/\s+/)
88
+ const e164 = phoneToE164(phone)
89
+ await updateCustomer({
90
+ first_name: first_name || "",
91
+ last_name: rest.join(" ") || ".",
92
+ phone: e164 || undefined,
93
+ })
94
+ if (e164 && phoneDigits(e164) !== phoneDigits(customer.phone || "")) {
95
+ setPhoneVerified(false)
96
+ }
97
+ return { ok: true as const, error: null as string | null }
98
+ } catch (err) {
99
+ if (isNextRedirect(err)) throw err
100
+ return {
101
+ ok: false as const,
102
+ error: safeErrorMessage(err, "Failed to update profile"),
103
+ }
104
+ }
105
+ }
106
+
107
+ const [state, formAction, pending] = useActionState(saveProfile, {
108
+ ok: false as const,
109
+ error: null as string | null,
110
+ })
111
+
112
+ async function ensurePhoneSavedForVerification(): Promise<boolean> {
113
+ setPhoneError(null)
114
+ if (!phoneReady) {
115
+ setPhoneError("Please enter a valid mobile number before verifying.")
116
+ return false
117
+ }
118
+
119
+ const e164 = phoneToE164(phone)
120
+ if (phoneDigits(e164) === phoneDigits(customer.phone || "")) {
121
+ return true
122
+ }
123
+
124
+ try {
125
+ await updateCustomer({ phone: e164 })
126
+ return true
127
+ } catch (err) {
128
+ if (isNextRedirect(err)) throw err
129
+ setPhoneError(
130
+ safeErrorMessage(err, "Failed to save mobile number. Please try again.")
131
+ )
132
+ return false
133
+ }
134
+ }
135
+
136
+ async function sendVerifyOtp(type: "email_verification" | "phone_verification") {
137
+ setOtpType(type)
138
+ setOtpPending(true)
139
+ setOtpError(null)
140
+
141
+ try {
142
+ if (type === "phone_verification") {
143
+ const saved = await ensurePhoneSavedForVerification()
144
+ if (!saved) return
145
+ setOtpFlow("verify_existing")
146
+ }
147
+
148
+ if (type === "email_verification") {
149
+ const resolved = resolveEmailOtpFlow({
150
+ currentEmail: customer.email,
151
+ draftEmail: email,
152
+ })
153
+ if (!resolved.ok) {
154
+ setEmailError(EMAIL_OTP_ERRORS[resolved.reason])
155
+ return
156
+ }
157
+ setOtpFlow(resolved.flow)
158
+ const res =
159
+ resolved.flow === "change_contact"
160
+ ? await requestContactChange({ email: resolved.email })
161
+ : await sendRegistrationOtp(customer.id, type)
162
+ if (!res?.success) {
163
+ throw new Error(res?.error || "Failed to send code")
164
+ }
165
+ setOtpToken(res.token ?? null)
166
+ setOtp("")
167
+ setOtpOpen(true)
168
+ return
169
+ }
170
+
171
+ const res = await sendRegistrationOtp(customer.id, type)
172
+ if (!res?.success) {
173
+ throw new Error(res?.error || "Failed to send code")
174
+ }
175
+ setOtpToken(res.token ?? null)
176
+ setOtp("")
177
+ setOtpOpen(true)
178
+ } catch (err) {
179
+ if (isNextRedirect(err)) throw err
180
+ const msg = safeErrorMessage(err, "Failed to send code")
181
+ setOtpError(msg)
182
+ if (type === "phone_verification") {
183
+ setPhoneError(msg)
184
+ }
185
+ if (type === "email_verification") {
186
+ setEmailError(msg)
187
+ }
188
+ } finally {
189
+ setOtpPending(false)
190
+ }
191
+ }
192
+
193
+ async function verifyOtp() {
194
+ if (otp.length < 6) return
195
+ setOtpPending(true)
196
+ setOtpError(null)
197
+ try {
198
+ if (otpType === "email_verification" && otpFlow === "change_contact") {
199
+ const res = await verifyContactChange({
200
+ otpToken: otpToken || "",
201
+ code: otp,
202
+ })
203
+ if (!res?.success) {
204
+ throw new Error(res?.error || "Invalid code")
205
+ }
206
+ setEmailVerified(true)
207
+ } else {
208
+ const res = await verifyRegistrationOtp({
209
+ otpToken: otpToken || "",
210
+ code: otp,
211
+ countryCode,
212
+ skipRedirect: true,
213
+ })
214
+ if (!res?.success) {
215
+ throw new Error(res?.error || "Invalid code")
216
+ }
217
+ if (otpType === "phone_verification") {
218
+ setPhoneVerified(true)
219
+ }
220
+ if (otpType === "email_verification") {
221
+ setEmailVerified(true)
222
+ }
223
+ }
224
+ setOtpOpen(false)
225
+ setMessage("Verification successful.")
226
+ window.location.reload()
227
+ } catch (err) {
228
+ if (isNextRedirect(err)) throw err
229
+ setOtpError(safeErrorMessage(err, "Verification failed"))
230
+ setOtpPending(false)
231
+ }
232
+ }
233
+
234
+ function handlePhoneChange(value: string) {
235
+ setPhone(value)
236
+ setPhoneError(null)
237
+ const nextE164 = phoneToE164(value)
238
+ if (
239
+ nextE164 &&
240
+ phoneDigits(nextE164) !== phoneDigits(customer.phone || "")
241
+ ) {
242
+ setPhoneVerified(false)
243
+ }
244
+ }
245
+
246
+ function handleEmailChange(value: string) {
247
+ setEmail(value)
248
+ setEmailError(null)
249
+ if (normalizeEmail(value) !== normalizeEmail(customer.email)) {
250
+ setEmailVerified(false)
251
+ }
252
+ }
253
+
254
+ const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
255
+
256
+ return {
257
+ fullName,
258
+ phone,
259
+ handlePhoneChange,
260
+ phoneReady,
261
+ phoneError,
262
+ phoneVerified,
263
+ email,
264
+ handleEmailChange,
265
+ emailReady,
266
+ emailError,
267
+ emailVerified,
268
+ message,
269
+ state,
270
+ formAction,
271
+ pending,
272
+ otpOpen,
273
+ setOtpOpen,
274
+ otp,
275
+ setOtp,
276
+ otpType,
277
+ otpError,
278
+ otpPending,
279
+ sendVerifyOtp,
280
+ verifyOtp,
281
+ }
282
+ }