@pradip1995/segment-login-template 0.5.24 → 0.5.26
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/package.json +54 -54
- package/src/account-addresses.tsx +287 -287
- package/src/account-guest-orders.tsx +98 -98
- package/src/account-orders.tsx +260 -260
- package/src/account-overview.tsx +133 -133
- package/src/account-payment-methods.tsx +328 -328
- package/src/account-profile-logic.ts +282 -282
- package/src/account-profile.tsx +111 -111
- package/src/account-theme.ts +91 -91
- package/src/account-utils.ts +20 -20
- package/src/auth-server.ts +442 -442
- package/src/commerce-auth-otp-modal.css +125 -125
- package/src/commerce-auth-otp-modal.tsx +121 -121
- package/src/forgot-password-form.tsx +109 -109
- package/src/google-auth-section.tsx +22 -22
- package/src/google-sign-in-section.tsx +67 -142
- package/src/guest-order-modal.tsx +215 -215
- package/src/index.ts +7 -7
- package/src/is-next-redirect.ts +60 -60
- package/src/login-brand-panel.tsx +158 -158
- package/src/login-form.tsx +469 -469
- package/src/manifest.ts +11 -11
- package/src/order-history-status.test.ts +224 -224
- package/src/order-history-status.ts +244 -244
- package/src/otp-component.ts +14 -14
- package/src/otp-input.tsx +46 -46
- package/src/otp-unified-form.tsx +333 -333
- package/src/otp-verification-modal.tsx +112 -112
- package/src/payment-methods-actions.ts +243 -243
- package/src/profile-email-field.tsx +116 -116
- package/src/profile-email.test.ts +182 -182
- package/src/profile-email.ts +54 -54
- package/src/profile-phone-field.tsx +119 -119
- package/src/register-form.tsx +339 -339
- package/src/reorder-checkout-address.ts +101 -101
- package/src/reset-password-form.tsx +103 -103
- package/src/reset-password-page.tsx +12 -12
- package/src/segment.tsx +272 -272
package/src/register-form.tsx
CHANGED
|
@@ -1,339 +1,339 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
-
import { useState } from "react"
|
|
5
|
-
import {
|
|
6
|
-
registerCustomer,
|
|
7
|
-
sendRegistrationOtp,
|
|
8
|
-
verifyRegistrationOtp,
|
|
9
|
-
} from "./auth-server"
|
|
10
|
-
import GoogleAuthSection from "./google-auth-section"
|
|
11
|
-
import OtpVerificationModal from "./otp-verification-modal"
|
|
12
|
-
import type { OtpVerificationComponent } from "./otp-component"
|
|
13
|
-
import { auth } from "./account-theme"
|
|
14
|
-
import { AuthDivider, AuthField, Ornament } from "./login-form"
|
|
15
|
-
|
|
16
|
-
type VerificationMethod = "email_verification" | "phone_verification"
|
|
17
|
-
type RegistrationStep = "form" | "otp" | "success"
|
|
18
|
-
|
|
19
|
-
export default function RegisterForm({
|
|
20
|
-
countryCode,
|
|
21
|
-
OtpComponent = OtpVerificationModal,
|
|
22
|
-
onLogin,
|
|
23
|
-
}: {
|
|
24
|
-
countryCode: string
|
|
25
|
-
OtpComponent?: OtpVerificationComponent
|
|
26
|
-
onLogin: () => void
|
|
27
|
-
}) {
|
|
28
|
-
const [step, setStep] = useState<RegistrationStep>("form")
|
|
29
|
-
const [verificationMethod, setVerificationMethod] =
|
|
30
|
-
useState<VerificationMethod>("email_verification")
|
|
31
|
-
const [form, setForm] = useState({
|
|
32
|
-
full_name: "",
|
|
33
|
-
email: "",
|
|
34
|
-
phone: "",
|
|
35
|
-
password: "",
|
|
36
|
-
})
|
|
37
|
-
const [customerId, setCustomerId] = useState<string | null>(null)
|
|
38
|
-
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
39
|
-
const [otp, setOtp] = useState("")
|
|
40
|
-
const [error, setError] = useState<string | null>(null)
|
|
41
|
-
const [pending, setPending] = useState(false)
|
|
42
|
-
const [showOtpModal, setShowOtpModal] = useState(false)
|
|
43
|
-
const [showPassword, setShowPassword] = useState(false)
|
|
44
|
-
|
|
45
|
-
function updateField(key: keyof typeof form, value: string) {
|
|
46
|
-
setForm((prev) => ({ ...prev, [key]: value }))
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async function handleRegister(e: React.FormEvent) {
|
|
50
|
-
e.preventDefault()
|
|
51
|
-
setPending(true)
|
|
52
|
-
setError(null)
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
if (verificationMethod === "phone_verification" && !form.phone.trim()) {
|
|
56
|
-
throw new Error("Phone number is required for mobile verification")
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const [first_name, ...rest] = form.full_name.trim().split(/\s+/)
|
|
60
|
-
const last_name = rest.join(" ") || "."
|
|
61
|
-
|
|
62
|
-
const result = await registerCustomer({
|
|
63
|
-
email: form.email.trim(),
|
|
64
|
-
first_name: first_name || "",
|
|
65
|
-
last_name,
|
|
66
|
-
phone: form.phone || undefined,
|
|
67
|
-
password: form.password,
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
if (!result.success) {
|
|
71
|
-
throw new Error(result.error || "Registration failed")
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const id = result.customer?.id
|
|
75
|
-
if (!id) {
|
|
76
|
-
throw new Error("Account was created but customer id is missing")
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
setCustomerId(id)
|
|
80
|
-
|
|
81
|
-
const otpResult = await sendRegistrationOtp(id, verificationMethod)
|
|
82
|
-
if (!otpResult.success) {
|
|
83
|
-
throw new Error(otpResult.error || "Failed to send verification code")
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
setOtpToken(otpResult.token ?? null)
|
|
87
|
-
setOtp("")
|
|
88
|
-
setStep("otp")
|
|
89
|
-
setShowOtpModal(true)
|
|
90
|
-
} catch (err) {
|
|
91
|
-
if (isNextRedirect(err)) throw err
|
|
92
|
-
setError(safeErrorMessage(err, "Registration failed"))
|
|
93
|
-
} finally {
|
|
94
|
-
setPending(false)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function handleVerifyOtp() {
|
|
99
|
-
if (!otp || otp.length < 6) return
|
|
100
|
-
|
|
101
|
-
setPending(true)
|
|
102
|
-
setError(null)
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
const result = await verifyRegistrationOtp({
|
|
106
|
-
otpToken: otpToken || "",
|
|
107
|
-
code: otp,
|
|
108
|
-
countryCode,
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
if (!result.success) {
|
|
112
|
-
throw new Error(result.error || "Invalid verification code")
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
setStep("success")
|
|
116
|
-
window.setTimeout(() => {
|
|
117
|
-
setShowOtpModal(false)
|
|
118
|
-
onLogin()
|
|
119
|
-
}, 2000)
|
|
120
|
-
} catch (err) {
|
|
121
|
-
if (isNextRedirect(err)) throw err
|
|
122
|
-
setError(safeErrorMessage(err, "Verification failed"))
|
|
123
|
-
} finally {
|
|
124
|
-
setPending(false)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async function handleResendOtp() {
|
|
129
|
-
if (!customerId) return
|
|
130
|
-
setPending(true)
|
|
131
|
-
setError(null)
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
const result = await sendRegistrationOtp(customerId, verificationMethod)
|
|
135
|
-
if (!result.success) {
|
|
136
|
-
throw new Error(result.error || "Failed to resend code")
|
|
137
|
-
}
|
|
138
|
-
setOtpToken(result.token ?? null)
|
|
139
|
-
setOtp("")
|
|
140
|
-
} catch (err) {
|
|
141
|
-
if (isNextRedirect(err)) throw err
|
|
142
|
-
setError(safeErrorMessage(err, "Failed to resend code"))
|
|
143
|
-
} finally {
|
|
144
|
-
setPending(false)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const verificationTarget =
|
|
149
|
-
verificationMethod === "email_verification" ? form.email : form.phone
|
|
150
|
-
|
|
151
|
-
return (
|
|
152
|
-
<div className="space-y-6" data-testid="register-page">
|
|
153
|
-
<div className="text-center lg:hidden">
|
|
154
|
-
<Ornament />
|
|
155
|
-
<h2 className={auth.mobileTitle}>Create account</h2>
|
|
156
|
-
<p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
|
|
157
|
-
Join our collection
|
|
158
|
-
</p>
|
|
159
|
-
</div>
|
|
160
|
-
|
|
161
|
-
<GoogleAuthSection countryCode={countryCode} />
|
|
162
|
-
|
|
163
|
-
<AuthDivider label="Or register with email" />
|
|
164
|
-
|
|
165
|
-
{step === "form" && (
|
|
166
|
-
<>
|
|
167
|
-
<div className="flex gap-2 p-1 bg-surface-muted/80">
|
|
168
|
-
<VerificationToggle
|
|
169
|
-
active={verificationMethod === "email_verification"}
|
|
170
|
-
onClick={() => setVerificationMethod("email_verification")}
|
|
171
|
-
>
|
|
172
|
-
Email OTP
|
|
173
|
-
</VerificationToggle>
|
|
174
|
-
<VerificationToggle
|
|
175
|
-
active={verificationMethod === "phone_verification"}
|
|
176
|
-
onClick={() => setVerificationMethod("phone_verification")}
|
|
177
|
-
>
|
|
178
|
-
Mobile OTP
|
|
179
|
-
</VerificationToggle>
|
|
180
|
-
</div>
|
|
181
|
-
|
|
182
|
-
<p className="text-xs text-muted -mt-2">
|
|
183
|
-
{verificationMethod === "email_verification"
|
|
184
|
-
? "We will send a verification code to your email after you register."
|
|
185
|
-
: "We will send a verification code to your mobile after you register."}
|
|
186
|
-
</p>
|
|
187
|
-
|
|
188
|
-
<form onSubmit={handleRegister} className="space-y-4">
|
|
189
|
-
<AuthField
|
|
190
|
-
label="Full name"
|
|
191
|
-
value={form.full_name}
|
|
192
|
-
onChange={(e) => updateField("full_name", e.target.value)}
|
|
193
|
-
autoComplete="name"
|
|
194
|
-
placeholder="Your full name"
|
|
195
|
-
/>
|
|
196
|
-
<AuthField
|
|
197
|
-
label="Email"
|
|
198
|
-
type="email"
|
|
199
|
-
value={form.email}
|
|
200
|
-
onChange={(e) => updateField("email", e.target.value)}
|
|
201
|
-
autoComplete="email"
|
|
202
|
-
placeholder="you@example.com"
|
|
203
|
-
/>
|
|
204
|
-
<AuthField
|
|
205
|
-
label="Phone"
|
|
206
|
-
type="tel"
|
|
207
|
-
value={form.phone}
|
|
208
|
-
onChange={(e) => updateField("phone", e.target.value)}
|
|
209
|
-
autoComplete="tel"
|
|
210
|
-
required={verificationMethod === "phone_verification"}
|
|
211
|
-
placeholder="Phone number"
|
|
212
|
-
/>
|
|
213
|
-
<div>
|
|
214
|
-
<span className={`${auth.fieldLabel} mb-1.5 block`}>Password</span>
|
|
215
|
-
<div className="relative">
|
|
216
|
-
<input
|
|
217
|
-
type={showPassword ? "text" : "password"}
|
|
218
|
-
value={form.password}
|
|
219
|
-
onChange={(e) => updateField("password", e.target.value)}
|
|
220
|
-
autoComplete="new-password"
|
|
221
|
-
required
|
|
222
|
-
placeholder="Create a password"
|
|
223
|
-
className={`${auth.input} pr-10`}
|
|
224
|
-
/>
|
|
225
|
-
<button
|
|
226
|
-
type="button"
|
|
227
|
-
onClick={() => setShowPassword((v) => !v)}
|
|
228
|
-
className="absolute inset-y-0 right-0 flex items-center text-muted hover:text-heading transition-colors"
|
|
229
|
-
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
230
|
-
>
|
|
231
|
-
{showPassword ? <EyeOffIcon /> : <EyeIcon />}
|
|
232
|
-
</button>
|
|
233
|
-
</div>
|
|
234
|
-
</div>
|
|
235
|
-
|
|
236
|
-
{error && (
|
|
237
|
-
<p className="text-sm text-brand-sale" role="alert">
|
|
238
|
-
{error}
|
|
239
|
-
</p>
|
|
240
|
-
)}
|
|
241
|
-
|
|
242
|
-
<button
|
|
243
|
-
type="submit"
|
|
244
|
-
disabled={pending}
|
|
245
|
-
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
246
|
-
>
|
|
247
|
-
<span className="relative z-[1]">
|
|
248
|
-
{pending ? "Creating account…" : "Create account"}
|
|
249
|
-
</span>
|
|
250
|
-
</button>
|
|
251
|
-
</form>
|
|
252
|
-
</>
|
|
253
|
-
)}
|
|
254
|
-
|
|
255
|
-
{step !== "form" && !showOtpModal && (
|
|
256
|
-
<p className="text-sm text-muted text-center">
|
|
257
|
-
Complete verification to activate your account.
|
|
258
|
-
</p>
|
|
259
|
-
)}
|
|
260
|
-
|
|
261
|
-
<p className="text-sm text-center text-muted">
|
|
262
|
-
Already have an account?{" "}
|
|
263
|
-
<button
|
|
264
|
-
type="button"
|
|
265
|
-
onClick={onLogin}
|
|
266
|
-
className="text-heading font-semibold hover:text-brand-accent transition-colors underline underline-offset-2"
|
|
267
|
-
>
|
|
268
|
-
Sign in
|
|
269
|
-
</button>
|
|
270
|
-
</p>
|
|
271
|
-
|
|
272
|
-
<OtpComponent
|
|
273
|
-
open={showOtpModal}
|
|
274
|
-
preventClose={step === "otp"}
|
|
275
|
-
title={
|
|
276
|
-
step === "success"
|
|
277
|
-
? "Account verified"
|
|
278
|
-
: verificationMethod === "email_verification"
|
|
279
|
-
? "Verify your email"
|
|
280
|
-
: "Verify your mobile"
|
|
281
|
-
}
|
|
282
|
-
description={
|
|
283
|
-
step === "success"
|
|
284
|
-
? "Your account is ready."
|
|
285
|
-
: `We've sent a 6-digit verification code to ${verificationTarget || "your contact"}.`
|
|
286
|
-
}
|
|
287
|
-
otp={otp}
|
|
288
|
-
onOtpChange={setOtp}
|
|
289
|
-
error={error}
|
|
290
|
-
pending={pending}
|
|
291
|
-
success={step === "success"}
|
|
292
|
-
successTitle="Account verified"
|
|
293
|
-
successMessage="Redirecting you to sign in…"
|
|
294
|
-
verifyLabel="Verify & continue"
|
|
295
|
-
onVerify={handleVerifyOtp}
|
|
296
|
-
onResend={handleResendOtp}
|
|
297
|
-
/>
|
|
298
|
-
</div>
|
|
299
|
-
)
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
function VerificationToggle({
|
|
304
|
-
active,
|
|
305
|
-
onClick,
|
|
306
|
-
children,
|
|
307
|
-
}: {
|
|
308
|
-
active: boolean
|
|
309
|
-
onClick: () => void
|
|
310
|
-
children: React.ReactNode
|
|
311
|
-
}) {
|
|
312
|
-
return (
|
|
313
|
-
<button
|
|
314
|
-
type="button"
|
|
315
|
-
onClick={onClick}
|
|
316
|
-
className={active ? auth.modeActive : auth.modeIdle}
|
|
317
|
-
>
|
|
318
|
-
{children}
|
|
319
|
-
</button>
|
|
320
|
-
)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function EyeIcon() {
|
|
324
|
-
return (
|
|
325
|
-
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
|
|
326
|
-
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
|
327
|
-
<circle cx="12" cy="12" r="3" />
|
|
328
|
-
</svg>
|
|
329
|
-
)
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function EyeOffIcon() {
|
|
333
|
-
return (
|
|
334
|
-
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
|
|
335
|
-
<path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24" />
|
|
336
|
-
<line x1="1" y1="1" x2="23" y2="23" />
|
|
337
|
-
</svg>
|
|
338
|
-
)
|
|
339
|
-
}
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import {
|
|
6
|
+
registerCustomer,
|
|
7
|
+
sendRegistrationOtp,
|
|
8
|
+
verifyRegistrationOtp,
|
|
9
|
+
} from "./auth-server"
|
|
10
|
+
import GoogleAuthSection from "./google-auth-section"
|
|
11
|
+
import OtpVerificationModal from "./otp-verification-modal"
|
|
12
|
+
import type { OtpVerificationComponent } from "./otp-component"
|
|
13
|
+
import { auth } from "./account-theme"
|
|
14
|
+
import { AuthDivider, AuthField, Ornament } from "./login-form"
|
|
15
|
+
|
|
16
|
+
type VerificationMethod = "email_verification" | "phone_verification"
|
|
17
|
+
type RegistrationStep = "form" | "otp" | "success"
|
|
18
|
+
|
|
19
|
+
export default function RegisterForm({
|
|
20
|
+
countryCode,
|
|
21
|
+
OtpComponent = OtpVerificationModal,
|
|
22
|
+
onLogin,
|
|
23
|
+
}: {
|
|
24
|
+
countryCode: string
|
|
25
|
+
OtpComponent?: OtpVerificationComponent
|
|
26
|
+
onLogin: () => void
|
|
27
|
+
}) {
|
|
28
|
+
const [step, setStep] = useState<RegistrationStep>("form")
|
|
29
|
+
const [verificationMethod, setVerificationMethod] =
|
|
30
|
+
useState<VerificationMethod>("email_verification")
|
|
31
|
+
const [form, setForm] = useState({
|
|
32
|
+
full_name: "",
|
|
33
|
+
email: "",
|
|
34
|
+
phone: "",
|
|
35
|
+
password: "",
|
|
36
|
+
})
|
|
37
|
+
const [customerId, setCustomerId] = useState<string | null>(null)
|
|
38
|
+
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
39
|
+
const [otp, setOtp] = useState("")
|
|
40
|
+
const [error, setError] = useState<string | null>(null)
|
|
41
|
+
const [pending, setPending] = useState(false)
|
|
42
|
+
const [showOtpModal, setShowOtpModal] = useState(false)
|
|
43
|
+
const [showPassword, setShowPassword] = useState(false)
|
|
44
|
+
|
|
45
|
+
function updateField(key: keyof typeof form, value: string) {
|
|
46
|
+
setForm((prev) => ({ ...prev, [key]: value }))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function handleRegister(e: React.FormEvent) {
|
|
50
|
+
e.preventDefault()
|
|
51
|
+
setPending(true)
|
|
52
|
+
setError(null)
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
if (verificationMethod === "phone_verification" && !form.phone.trim()) {
|
|
56
|
+
throw new Error("Phone number is required for mobile verification")
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const [first_name, ...rest] = form.full_name.trim().split(/\s+/)
|
|
60
|
+
const last_name = rest.join(" ") || "."
|
|
61
|
+
|
|
62
|
+
const result = await registerCustomer({
|
|
63
|
+
email: form.email.trim(),
|
|
64
|
+
first_name: first_name || "",
|
|
65
|
+
last_name,
|
|
66
|
+
phone: form.phone || undefined,
|
|
67
|
+
password: form.password,
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
if (!result.success) {
|
|
71
|
+
throw new Error(result.error || "Registration failed")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const id = result.customer?.id
|
|
75
|
+
if (!id) {
|
|
76
|
+
throw new Error("Account was created but customer id is missing")
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
setCustomerId(id)
|
|
80
|
+
|
|
81
|
+
const otpResult = await sendRegistrationOtp(id, verificationMethod)
|
|
82
|
+
if (!otpResult.success) {
|
|
83
|
+
throw new Error(otpResult.error || "Failed to send verification code")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
setOtpToken(otpResult.token ?? null)
|
|
87
|
+
setOtp("")
|
|
88
|
+
setStep("otp")
|
|
89
|
+
setShowOtpModal(true)
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (isNextRedirect(err)) throw err
|
|
92
|
+
setError(safeErrorMessage(err, "Registration failed"))
|
|
93
|
+
} finally {
|
|
94
|
+
setPending(false)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function handleVerifyOtp() {
|
|
99
|
+
if (!otp || otp.length < 6) return
|
|
100
|
+
|
|
101
|
+
setPending(true)
|
|
102
|
+
setError(null)
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
const result = await verifyRegistrationOtp({
|
|
106
|
+
otpToken: otpToken || "",
|
|
107
|
+
code: otp,
|
|
108
|
+
countryCode,
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
throw new Error(result.error || "Invalid verification code")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
setStep("success")
|
|
116
|
+
window.setTimeout(() => {
|
|
117
|
+
setShowOtpModal(false)
|
|
118
|
+
onLogin()
|
|
119
|
+
}, 2000)
|
|
120
|
+
} catch (err) {
|
|
121
|
+
if (isNextRedirect(err)) throw err
|
|
122
|
+
setError(safeErrorMessage(err, "Verification failed"))
|
|
123
|
+
} finally {
|
|
124
|
+
setPending(false)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function handleResendOtp() {
|
|
129
|
+
if (!customerId) return
|
|
130
|
+
setPending(true)
|
|
131
|
+
setError(null)
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const result = await sendRegistrationOtp(customerId, verificationMethod)
|
|
135
|
+
if (!result.success) {
|
|
136
|
+
throw new Error(result.error || "Failed to resend code")
|
|
137
|
+
}
|
|
138
|
+
setOtpToken(result.token ?? null)
|
|
139
|
+
setOtp("")
|
|
140
|
+
} catch (err) {
|
|
141
|
+
if (isNextRedirect(err)) throw err
|
|
142
|
+
setError(safeErrorMessage(err, "Failed to resend code"))
|
|
143
|
+
} finally {
|
|
144
|
+
setPending(false)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const verificationTarget =
|
|
149
|
+
verificationMethod === "email_verification" ? form.email : form.phone
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<div className="space-y-6" data-testid="register-page">
|
|
153
|
+
<div className="text-center lg:hidden">
|
|
154
|
+
<Ornament />
|
|
155
|
+
<h2 className={auth.mobileTitle}>Create account</h2>
|
|
156
|
+
<p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
|
|
157
|
+
Join our collection
|
|
158
|
+
</p>
|
|
159
|
+
</div>
|
|
160
|
+
|
|
161
|
+
<GoogleAuthSection countryCode={countryCode} />
|
|
162
|
+
|
|
163
|
+
<AuthDivider label="Or register with email" />
|
|
164
|
+
|
|
165
|
+
{step === "form" && (
|
|
166
|
+
<>
|
|
167
|
+
<div className="flex gap-2 p-1 bg-surface-muted/80">
|
|
168
|
+
<VerificationToggle
|
|
169
|
+
active={verificationMethod === "email_verification"}
|
|
170
|
+
onClick={() => setVerificationMethod("email_verification")}
|
|
171
|
+
>
|
|
172
|
+
Email OTP
|
|
173
|
+
</VerificationToggle>
|
|
174
|
+
<VerificationToggle
|
|
175
|
+
active={verificationMethod === "phone_verification"}
|
|
176
|
+
onClick={() => setVerificationMethod("phone_verification")}
|
|
177
|
+
>
|
|
178
|
+
Mobile OTP
|
|
179
|
+
</VerificationToggle>
|
|
180
|
+
</div>
|
|
181
|
+
|
|
182
|
+
<p className="text-xs text-muted -mt-2">
|
|
183
|
+
{verificationMethod === "email_verification"
|
|
184
|
+
? "We will send a verification code to your email after you register."
|
|
185
|
+
: "We will send a verification code to your mobile after you register."}
|
|
186
|
+
</p>
|
|
187
|
+
|
|
188
|
+
<form onSubmit={handleRegister} className="space-y-4">
|
|
189
|
+
<AuthField
|
|
190
|
+
label="Full name"
|
|
191
|
+
value={form.full_name}
|
|
192
|
+
onChange={(e) => updateField("full_name", e.target.value)}
|
|
193
|
+
autoComplete="name"
|
|
194
|
+
placeholder="Your full name"
|
|
195
|
+
/>
|
|
196
|
+
<AuthField
|
|
197
|
+
label="Email"
|
|
198
|
+
type="email"
|
|
199
|
+
value={form.email}
|
|
200
|
+
onChange={(e) => updateField("email", e.target.value)}
|
|
201
|
+
autoComplete="email"
|
|
202
|
+
placeholder="you@example.com"
|
|
203
|
+
/>
|
|
204
|
+
<AuthField
|
|
205
|
+
label="Phone"
|
|
206
|
+
type="tel"
|
|
207
|
+
value={form.phone}
|
|
208
|
+
onChange={(e) => updateField("phone", e.target.value)}
|
|
209
|
+
autoComplete="tel"
|
|
210
|
+
required={verificationMethod === "phone_verification"}
|
|
211
|
+
placeholder="Phone number"
|
|
212
|
+
/>
|
|
213
|
+
<div>
|
|
214
|
+
<span className={`${auth.fieldLabel} mb-1.5 block`}>Password</span>
|
|
215
|
+
<div className="relative">
|
|
216
|
+
<input
|
|
217
|
+
type={showPassword ? "text" : "password"}
|
|
218
|
+
value={form.password}
|
|
219
|
+
onChange={(e) => updateField("password", e.target.value)}
|
|
220
|
+
autoComplete="new-password"
|
|
221
|
+
required
|
|
222
|
+
placeholder="Create a password"
|
|
223
|
+
className={`${auth.input} pr-10`}
|
|
224
|
+
/>
|
|
225
|
+
<button
|
|
226
|
+
type="button"
|
|
227
|
+
onClick={() => setShowPassword((v) => !v)}
|
|
228
|
+
className="absolute inset-y-0 right-0 flex items-center text-muted hover:text-heading transition-colors"
|
|
229
|
+
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
230
|
+
>
|
|
231
|
+
{showPassword ? <EyeOffIcon /> : <EyeIcon />}
|
|
232
|
+
</button>
|
|
233
|
+
</div>
|
|
234
|
+
</div>
|
|
235
|
+
|
|
236
|
+
{error && (
|
|
237
|
+
<p className="text-sm text-brand-sale" role="alert">
|
|
238
|
+
{error}
|
|
239
|
+
</p>
|
|
240
|
+
)}
|
|
241
|
+
|
|
242
|
+
<button
|
|
243
|
+
type="submit"
|
|
244
|
+
disabled={pending}
|
|
245
|
+
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
246
|
+
>
|
|
247
|
+
<span className="relative z-[1]">
|
|
248
|
+
{pending ? "Creating account…" : "Create account"}
|
|
249
|
+
</span>
|
|
250
|
+
</button>
|
|
251
|
+
</form>
|
|
252
|
+
</>
|
|
253
|
+
)}
|
|
254
|
+
|
|
255
|
+
{step !== "form" && !showOtpModal && (
|
|
256
|
+
<p className="text-sm text-muted text-center">
|
|
257
|
+
Complete verification to activate your account.
|
|
258
|
+
</p>
|
|
259
|
+
)}
|
|
260
|
+
|
|
261
|
+
<p className="text-sm text-center text-muted">
|
|
262
|
+
Already have an account?{" "}
|
|
263
|
+
<button
|
|
264
|
+
type="button"
|
|
265
|
+
onClick={onLogin}
|
|
266
|
+
className="text-heading font-semibold hover:text-brand-accent transition-colors underline underline-offset-2"
|
|
267
|
+
>
|
|
268
|
+
Sign in
|
|
269
|
+
</button>
|
|
270
|
+
</p>
|
|
271
|
+
|
|
272
|
+
<OtpComponent
|
|
273
|
+
open={showOtpModal}
|
|
274
|
+
preventClose={step === "otp"}
|
|
275
|
+
title={
|
|
276
|
+
step === "success"
|
|
277
|
+
? "Account verified"
|
|
278
|
+
: verificationMethod === "email_verification"
|
|
279
|
+
? "Verify your email"
|
|
280
|
+
: "Verify your mobile"
|
|
281
|
+
}
|
|
282
|
+
description={
|
|
283
|
+
step === "success"
|
|
284
|
+
? "Your account is ready."
|
|
285
|
+
: `We've sent a 6-digit verification code to ${verificationTarget || "your contact"}.`
|
|
286
|
+
}
|
|
287
|
+
otp={otp}
|
|
288
|
+
onOtpChange={setOtp}
|
|
289
|
+
error={error}
|
|
290
|
+
pending={pending}
|
|
291
|
+
success={step === "success"}
|
|
292
|
+
successTitle="Account verified"
|
|
293
|
+
successMessage="Redirecting you to sign in…"
|
|
294
|
+
verifyLabel="Verify & continue"
|
|
295
|
+
onVerify={handleVerifyOtp}
|
|
296
|
+
onResend={handleResendOtp}
|
|
297
|
+
/>
|
|
298
|
+
</div>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
function VerificationToggle({
|
|
304
|
+
active,
|
|
305
|
+
onClick,
|
|
306
|
+
children,
|
|
307
|
+
}: {
|
|
308
|
+
active: boolean
|
|
309
|
+
onClick: () => void
|
|
310
|
+
children: React.ReactNode
|
|
311
|
+
}) {
|
|
312
|
+
return (
|
|
313
|
+
<button
|
|
314
|
+
type="button"
|
|
315
|
+
onClick={onClick}
|
|
316
|
+
className={active ? auth.modeActive : auth.modeIdle}
|
|
317
|
+
>
|
|
318
|
+
{children}
|
|
319
|
+
</button>
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function EyeIcon() {
|
|
324
|
+
return (
|
|
325
|
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
|
|
326
|
+
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
|
327
|
+
<circle cx="12" cy="12" r="3" />
|
|
328
|
+
</svg>
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function EyeOffIcon() {
|
|
333
|
+
return (
|
|
334
|
+
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
|
|
335
|
+
<path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24" />
|
|
336
|
+
<line x1="1" y1="1" x2="23" y2="23" />
|
|
337
|
+
</svg>
|
|
338
|
+
)
|
|
339
|
+
}
|