@pradip1995/segment-login-template 0.5.25 → 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.
Files changed (38) hide show
  1. package/package.json +54 -54
  2. package/src/account-addresses.tsx +287 -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/auth-server.ts +442 -442
  12. package/src/commerce-auth-otp-modal.css +125 -125
  13. package/src/commerce-auth-otp-modal.tsx +121 -121
  14. package/src/forgot-password-form.tsx +109 -109
  15. package/src/google-auth-section.tsx +22 -22
  16. package/src/google-sign-in-section.tsx +67 -67
  17. package/src/guest-order-modal.tsx +215 -215
  18. package/src/index.ts +7 -7
  19. package/src/is-next-redirect.ts +60 -60
  20. package/src/login-brand-panel.tsx +158 -158
  21. package/src/login-form.tsx +469 -469
  22. package/src/manifest.ts +11 -11
  23. package/src/order-history-status.test.ts +224 -224
  24. package/src/order-history-status.ts +244 -244
  25. package/src/otp-component.ts +14 -14
  26. package/src/otp-input.tsx +46 -46
  27. package/src/otp-unified-form.tsx +333 -333
  28. package/src/otp-verification-modal.tsx +112 -112
  29. package/src/payment-methods-actions.ts +243 -243
  30. package/src/profile-email-field.tsx +116 -116
  31. package/src/profile-email.test.ts +182 -182
  32. package/src/profile-email.ts +54 -54
  33. package/src/profile-phone-field.tsx +119 -119
  34. package/src/register-form.tsx +339 -339
  35. package/src/reorder-checkout-address.ts +101 -101
  36. package/src/reset-password-form.tsx +103 -103
  37. package/src/reset-password-page.tsx +12 -12
  38. package/src/segment.tsx +272 -272
@@ -1,469 +1,469 @@
1
- "use client"
2
-
3
- import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
- import { useActionState, useState } from "react"
5
- import { login } from "@pradip1995/commerce-core/client/actions/customer"
6
- import { sendAuthOtp, verifyAuthOtpAndLogin } from "./auth-server"
7
- import GoogleAuthSection from "./google-auth-section"
8
- import OtpVerificationModal from "./otp-verification-modal"
9
- import type { OtpVerificationComponent } from "./otp-component"
10
- import { auth } from "./account-theme"
11
-
12
- type LoginMode = "password" | "otp"
13
-
14
- export default function LoginForm({
15
- countryCode,
16
- OtpComponent = OtpVerificationModal,
17
- onForgot,
18
- onRegister,
19
- onTrackGuest,
20
- }: {
21
- countryCode: string
22
- OtpComponent?: OtpVerificationComponent
23
- onForgot: () => void
24
- onRegister: () => void
25
- onTrackGuest?: () => void
26
- }) {
27
- const [mode, setMode] = useState<LoginMode>("password")
28
-
29
- return (
30
- <div className="w-full flex flex-col space-y-6" data-testid="login-page">
31
- <div className="text-center lg:hidden">
32
- <Ornament />
33
- <h2 className={auth.mobileTitle}>Sign in</h2>
34
- <p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
35
- Access your account
36
- </p>
37
- </div>
38
-
39
- <GoogleAuthSection countryCode={countryCode} />
40
-
41
- <AuthDivider label="Or continue with" />
42
-
43
- <div className="flex gap-2 p-1 bg-surface-muted/80">
44
- <ModeButton active={mode === "password"} onClick={() => setMode("password")}>
45
- Password
46
- </ModeButton>
47
- <ModeButton active={mode === "otp"} onClick={() => setMode("otp")}>
48
- OTP
49
- </ModeButton>
50
- </div>
51
-
52
- {mode === "password" ? (
53
- <PasswordLoginForm countryCode={countryCode} onForgot={onForgot} />
54
- ) : (
55
- <OtpLoginForm countryCode={countryCode} OtpComponent={OtpComponent} />
56
- )}
57
-
58
- <p className="text-sm text-center text-muted">
59
- New here?{" "}
60
- <button
61
- type="button"
62
- onClick={onRegister}
63
- className="text-heading font-semibold hover:text-brand-accent transition-colors underline underline-offset-2"
64
- >
65
- Create an account
66
- </button>
67
- </p>
68
-
69
- {onTrackGuest && (
70
- <div className="pt-4 border-t border-brand-primary/10">
71
- <button
72
- type="button"
73
- onClick={onTrackGuest}
74
- className={auth.guestCta}
75
- data-testid="track-guest-order-button"
76
- >
77
- <GuestPackageIcon />
78
- Track guest order
79
- </button>
80
- </div>
81
- )}
82
- </div>
83
- )
84
- }
85
-
86
- function ModeButton({
87
- active,
88
- onClick,
89
- children,
90
- }: {
91
- active: boolean
92
- onClick: () => void
93
- children: React.ReactNode
94
- }) {
95
- return (
96
- <button type="button" onClick={onClick} className={active ? auth.modeActive : auth.modeIdle}>
97
- {children}
98
- </button>
99
- )
100
- }
101
-
102
- function PasswordLoginForm({
103
- countryCode,
104
- onForgot,
105
- }: {
106
- countryCode: string
107
- onForgot: () => void
108
- }) {
109
- const [state, formAction, pending] = useActionState(login, null)
110
- const [showPassword, setShowPassword] = useState(false)
111
-
112
- return (
113
- <form action={formAction} className="space-y-5">
114
- <input type="hidden" name="country_code" value={countryCode} />
115
-
116
- <AuthField
117
- label="Email or phone"
118
- name="email_or_phone"
119
- type="text"
120
- autoComplete="username"
121
- placeholder="Enter email or phone"
122
- />
123
-
124
- <div>
125
- <div className="flex items-center justify-between mb-1.5">
126
- <span className={auth.fieldLabel}>Password</span>
127
- <button
128
- type="button"
129
- onClick={onForgot}
130
- className={`${auth.fieldLabel} text-brand-accent hover:text-heading transition-colors`}
131
- >
132
- Forgot?
133
- </button>
134
- </div>
135
- <div className="relative">
136
- <input
137
- name="password"
138
- type={showPassword ? "text" : "password"}
139
- autoComplete="current-password"
140
- required
141
- placeholder="Enter your password"
142
- className={`${auth.input} pr-10`}
143
- data-testid="password-input"
144
- />
145
- <button
146
- type="button"
147
- onClick={() => setShowPassword((v) => !v)}
148
- className="absolute inset-y-0 right-0 flex items-center text-muted hover:text-heading transition-colors"
149
- aria-label={showPassword ? "Hide password" : "Show password"}
150
- >
151
- {showPassword ? <EyeOffIcon /> : <EyeIcon />}
152
- </button>
153
- </div>
154
- </div>
155
-
156
- {state && typeof state === "string" && state !== "ACCOUNT_DELETION_PENDING" && (
157
- <p className="text-sm text-brand-sale" role="alert">
158
- {state === "Email not verified." || state.includes("not verified")
159
- ? "Your email is not verified yet. Complete OTP verification during registration, or use OTP sign-in below."
160
- : state}
161
- </p>
162
- )}
163
- {state === "ACCOUNT_DELETION_PENDING" && (
164
- <p className="text-sm text-brand-sale" role="alert">
165
- This account has a pending deletion request. Check your email to cancel it.
166
- </p>
167
- )}
168
-
169
- <button
170
- type="submit"
171
- disabled={pending}
172
- className={`${auth.btnPrimary} ${auth.btnShine}`}
173
- data-testid="sign-in-button"
174
- >
175
- <span className="relative z-[1]">{pending ? "Signing in…" : "Sign in"}</span>
176
- </button>
177
- </form>
178
- )
179
- }
180
-
181
- function OtpLoginForm({
182
- countryCode,
183
- OtpComponent,
184
- }: {
185
- countryCode: string
186
- OtpComponent: OtpVerificationComponent
187
- }) {
188
- const [authMethod, setAuthMethod] = useState<"email_auth" | "phone_auth">("email_auth")
189
- const [identifier, setIdentifier] = useState("")
190
- const [otpToken, setOtpToken] = useState<string | null>(null)
191
- const [otp, setOtp] = useState("")
192
- const [showOtpModal, setShowOtpModal] = useState(false)
193
- const [error, setError] = useState<string | null>(null)
194
- const [pending, setPending] = useState(false)
195
- const [isNewUser, setIsNewUser] = useState(false)
196
- const [firstName, setFirstName] = useState("")
197
- const [lastName, setLastName] = useState("")
198
-
199
- async function handleSendOtp(e: React.FormEvent) {
200
- e.preventDefault()
201
- setPending(true)
202
- setError(null)
203
-
204
- try {
205
- const res = await sendAuthOtp(
206
- authMethod === "email_auth"
207
- ? { email: identifier.trim(), type: "email_auth" }
208
- : { phone: identifier.replace(/[^\d+]/g, ""), type: "phone_auth" }
209
- )
210
-
211
- if (!res.success) {
212
- throw new Error(res.error)
213
- }
214
-
215
- setOtpToken(res.token ?? null)
216
- setIsNewUser(res.isNewUser ?? false)
217
- setOtp("")
218
- setShowOtpModal(true)
219
- } catch (err) {
220
- if (isNextRedirect(err)) throw err
221
- setError(safeErrorMessage(err, "Failed to send code"))
222
- } finally {
223
- setPending(false)
224
- }
225
- }
226
-
227
- async function handleResendOtp() {
228
- setPending(true)
229
- setError(null)
230
-
231
- try {
232
- const res = await sendAuthOtp(
233
- authMethod === "email_auth"
234
- ? { email: identifier.trim(), type: "email_auth" }
235
- : { phone: identifier.replace(/[^\d+]/g, ""), type: "phone_auth" }
236
- )
237
-
238
- if (!res.success) {
239
- throw new Error(res.error)
240
- }
241
-
242
- setOtpToken(res.token ?? null)
243
- setOtp("")
244
- } catch (err) {
245
- setError(safeErrorMessage(err, "Failed to resend code"))
246
- } finally {
247
- setPending(false)
248
- }
249
- }
250
-
251
- async function handleVerifyOtp() {
252
- if (!otp || otp.length < 6) return
253
-
254
- setPending(true)
255
- setError(null)
256
-
257
- try {
258
- if (isNewUser && (!firstName.trim() || !lastName.trim())) {
259
- throw new Error("Please enter your first and last name to finish registration")
260
- }
261
-
262
- const result = await verifyAuthOtpAndLogin({
263
- token: otpToken || "",
264
- code: otp,
265
- countryCode,
266
- first_name: isNewUser ? firstName.trim() : undefined,
267
- last_name: isNewUser ? lastName.trim() : undefined,
268
- })
269
- if (result && !result.success) {
270
- throw new Error(result.error || "Verification failed")
271
- }
272
- } catch (err) {
273
- if (isNextRedirect(err)) throw err
274
- setError(safeErrorMessage(err, "Verification failed"))
275
- setPending(false)
276
- }
277
- }
278
-
279
- return (
280
- <>
281
- <form onSubmit={handleSendOtp} className="space-y-4">
282
- <div className="flex flex-wrap gap-2">
283
- <Chip active={authMethod === "email_auth"} onClick={() => setAuthMethod("email_auth")}>
284
- Email OTP
285
- </Chip>
286
- <Chip active={authMethod === "phone_auth"} onClick={() => setAuthMethod("phone_auth")}>
287
- Phone OTP
288
- </Chip>
289
- </div>
290
-
291
- <AuthField
292
- label={authMethod === "phone_auth" ? "Phone" : "Email"}
293
- name="identifier"
294
- type={authMethod === "phone_auth" ? "tel" : "email"}
295
- value={identifier}
296
- onChange={(e) => setIdentifier(e.target.value)}
297
- autoComplete={authMethod === "phone_auth" ? "tel" : "email"}
298
- placeholder={
299
- authMethod === "phone_auth" ? "Enter your phone" : "Enter your email"
300
- }
301
- />
302
-
303
- <p className="text-xs text-muted">
304
- We will send a one-time code to sign in or create your account.
305
- </p>
306
-
307
- {error && !showOtpModal && (
308
- <p className="text-sm text-brand-sale" role="alert">
309
- {error}
310
- </p>
311
- )}
312
-
313
- <button
314
- type="submit"
315
- disabled={pending || !identifier.trim()}
316
- className={`${auth.btnPrimary} ${auth.btnShine}`}
317
- >
318
- <span className="relative z-[1]">
319
- {pending ? "Sending…" : "Send verification code"}
320
- </span>
321
- </button>
322
- </form>
323
-
324
- <OtpComponent
325
- open={showOtpModal}
326
- preventClose
327
- title={authMethod === "phone_auth" ? "Verify your phone" : "Verify your email"}
328
- description={`Enter the 6-digit code sent to ${identifier || "your contact"}.`}
329
- otp={otp}
330
- onOtpChange={setOtp}
331
- error={error}
332
- pending={pending}
333
- verifyLabel="Verify & sign in"
334
- onVerify={handleVerifyOtp}
335
- onResend={handleResendOtp}
336
- extraContent={
337
- isNewUser ? (
338
- <div className="grid grid-cols-2 gap-3 mb-6">
339
- <AuthField
340
- label="First name"
341
- name="first_name"
342
- value={firstName}
343
- onChange={(e) => setFirstName(e.target.value)}
344
- required
345
- />
346
- <AuthField
347
- label="Last name"
348
- name="last_name"
349
- value={lastName}
350
- onChange={(e) => setLastName(e.target.value)}
351
- required
352
- />
353
- </div>
354
- ) : undefined
355
- }
356
- />
357
- </>
358
- )
359
- }
360
-
361
- export function AuthDivider({ label }: { label: string }) {
362
- return (
363
- <div className="relative">
364
- <div className="absolute inset-0 flex items-center">
365
- <div className="w-full border-t border-brand-primary/10" />
366
- </div>
367
- <div className="relative flex justify-center items-center gap-2 text-[10px] font-semibold uppercase tracking-[0.28em]">
368
- <span className="px-2 bg-surface text-brand-accent">◆</span>
369
- <span className="px-1 bg-surface text-muted">{label}</span>
370
- <span className="px-2 bg-surface text-brand-accent">◆</span>
371
- </div>
372
- </div>
373
- )
374
- }
375
-
376
- export function AuthField({
377
- label,
378
- name,
379
- type = "text",
380
- autoComplete,
381
- value,
382
- onChange,
383
- placeholder,
384
- required = true,
385
- }: {
386
- label: string
387
- name?: string
388
- type?: string
389
- autoComplete?: string
390
- value?: string
391
- onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void
392
- placeholder?: string
393
- required?: boolean
394
- }) {
395
- return (
396
- <label className="block">
397
- <span className={`${auth.fieldLabel} mb-1.5 block`}>{label}</span>
398
- <input
399
- name={name}
400
- type={type}
401
- autoComplete={autoComplete}
402
- value={value}
403
- onChange={onChange}
404
- placeholder={placeholder}
405
- required={required}
406
- className={auth.input}
407
- />
408
- </label>
409
- )
410
- }
411
-
412
- export function Ornament() {
413
- return (
414
- <div className="flex items-center justify-center gap-2 mb-3" aria-hidden>
415
- <span className="h-px w-6 bg-brand-accent/70" />
416
- <span className="text-brand-accent text-[7px] leading-none">◆</span>
417
- <span className="h-px w-6 bg-brand-accent/70" />
418
- </div>
419
- )
420
- }
421
-
422
- function Chip({
423
- active,
424
- onClick,
425
- children,
426
- }: {
427
- active: boolean
428
- onClick: () => void
429
- children: React.ReactNode
430
- }) {
431
- return (
432
- <button
433
- type="button"
434
- onClick={onClick}
435
- className={active ? auth.chipActive : auth.chipIdle}
436
- >
437
- {children}
438
- </button>
439
- )
440
- }
441
-
442
-
443
- function GuestPackageIcon() {
444
- return (
445
- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
446
- <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" strokeLinecap="round" strokeLinejoin="round" />
447
- <polyline points="3.27 6.96 12 12.01 20.73 6.96" strokeLinecap="round" strokeLinejoin="round" />
448
- <line x1="12" y1="22.08" x2="12" y2="12" strokeLinecap="round" />
449
- </svg>
450
- )
451
- }
452
-
453
- function EyeIcon() {
454
- return (
455
- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
456
- <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
457
- <circle cx="12" cy="12" r="3" />
458
- </svg>
459
- )
460
- }
461
-
462
- function EyeOffIcon() {
463
- return (
464
- <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
465
- <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" />
466
- <line x1="1" y1="1" x2="23" y2="23" />
467
- </svg>
468
- )
469
- }
1
+ "use client"
2
+
3
+ import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
+ import { useActionState, useState } from "react"
5
+ import { login } from "@pradip1995/commerce-core/client/actions/customer"
6
+ import { sendAuthOtp, verifyAuthOtpAndLogin } from "./auth-server"
7
+ import GoogleAuthSection from "./google-auth-section"
8
+ import OtpVerificationModal from "./otp-verification-modal"
9
+ import type { OtpVerificationComponent } from "./otp-component"
10
+ import { auth } from "./account-theme"
11
+
12
+ type LoginMode = "password" | "otp"
13
+
14
+ export default function LoginForm({
15
+ countryCode,
16
+ OtpComponent = OtpVerificationModal,
17
+ onForgot,
18
+ onRegister,
19
+ onTrackGuest,
20
+ }: {
21
+ countryCode: string
22
+ OtpComponent?: OtpVerificationComponent
23
+ onForgot: () => void
24
+ onRegister: () => void
25
+ onTrackGuest?: () => void
26
+ }) {
27
+ const [mode, setMode] = useState<LoginMode>("password")
28
+
29
+ return (
30
+ <div className="w-full flex flex-col space-y-6" data-testid="login-page">
31
+ <div className="text-center lg:hidden">
32
+ <Ornament />
33
+ <h2 className={auth.mobileTitle}>Sign in</h2>
34
+ <p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
35
+ Access your account
36
+ </p>
37
+ </div>
38
+
39
+ <GoogleAuthSection countryCode={countryCode} />
40
+
41
+ <AuthDivider label="Or continue with" />
42
+
43
+ <div className="flex gap-2 p-1 bg-surface-muted/80">
44
+ <ModeButton active={mode === "password"} onClick={() => setMode("password")}>
45
+ Password
46
+ </ModeButton>
47
+ <ModeButton active={mode === "otp"} onClick={() => setMode("otp")}>
48
+ OTP
49
+ </ModeButton>
50
+ </div>
51
+
52
+ {mode === "password" ? (
53
+ <PasswordLoginForm countryCode={countryCode} onForgot={onForgot} />
54
+ ) : (
55
+ <OtpLoginForm countryCode={countryCode} OtpComponent={OtpComponent} />
56
+ )}
57
+
58
+ <p className="text-sm text-center text-muted">
59
+ New here?{" "}
60
+ <button
61
+ type="button"
62
+ onClick={onRegister}
63
+ className="text-heading font-semibold hover:text-brand-accent transition-colors underline underline-offset-2"
64
+ >
65
+ Create an account
66
+ </button>
67
+ </p>
68
+
69
+ {onTrackGuest && (
70
+ <div className="pt-4 border-t border-brand-primary/10">
71
+ <button
72
+ type="button"
73
+ onClick={onTrackGuest}
74
+ className={auth.guestCta}
75
+ data-testid="track-guest-order-button"
76
+ >
77
+ <GuestPackageIcon />
78
+ Track guest order
79
+ </button>
80
+ </div>
81
+ )}
82
+ </div>
83
+ )
84
+ }
85
+
86
+ function ModeButton({
87
+ active,
88
+ onClick,
89
+ children,
90
+ }: {
91
+ active: boolean
92
+ onClick: () => void
93
+ children: React.ReactNode
94
+ }) {
95
+ return (
96
+ <button type="button" onClick={onClick} className={active ? auth.modeActive : auth.modeIdle}>
97
+ {children}
98
+ </button>
99
+ )
100
+ }
101
+
102
+ function PasswordLoginForm({
103
+ countryCode,
104
+ onForgot,
105
+ }: {
106
+ countryCode: string
107
+ onForgot: () => void
108
+ }) {
109
+ const [state, formAction, pending] = useActionState(login, null)
110
+ const [showPassword, setShowPassword] = useState(false)
111
+
112
+ return (
113
+ <form action={formAction} className="space-y-5">
114
+ <input type="hidden" name="country_code" value={countryCode} />
115
+
116
+ <AuthField
117
+ label="Email or phone"
118
+ name="email_or_phone"
119
+ type="text"
120
+ autoComplete="username"
121
+ placeholder="Enter email or phone"
122
+ />
123
+
124
+ <div>
125
+ <div className="flex items-center justify-between mb-1.5">
126
+ <span className={auth.fieldLabel}>Password</span>
127
+ <button
128
+ type="button"
129
+ onClick={onForgot}
130
+ className={`${auth.fieldLabel} text-brand-accent hover:text-heading transition-colors`}
131
+ >
132
+ Forgot?
133
+ </button>
134
+ </div>
135
+ <div className="relative">
136
+ <input
137
+ name="password"
138
+ type={showPassword ? "text" : "password"}
139
+ autoComplete="current-password"
140
+ required
141
+ placeholder="Enter your password"
142
+ className={`${auth.input} pr-10`}
143
+ data-testid="password-input"
144
+ />
145
+ <button
146
+ type="button"
147
+ onClick={() => setShowPassword((v) => !v)}
148
+ className="absolute inset-y-0 right-0 flex items-center text-muted hover:text-heading transition-colors"
149
+ aria-label={showPassword ? "Hide password" : "Show password"}
150
+ >
151
+ {showPassword ? <EyeOffIcon /> : <EyeIcon />}
152
+ </button>
153
+ </div>
154
+ </div>
155
+
156
+ {state && typeof state === "string" && state !== "ACCOUNT_DELETION_PENDING" && (
157
+ <p className="text-sm text-brand-sale" role="alert">
158
+ {state === "Email not verified." || state.includes("not verified")
159
+ ? "Your email is not verified yet. Complete OTP verification during registration, or use OTP sign-in below."
160
+ : state}
161
+ </p>
162
+ )}
163
+ {state === "ACCOUNT_DELETION_PENDING" && (
164
+ <p className="text-sm text-brand-sale" role="alert">
165
+ This account has a pending deletion request. Check your email to cancel it.
166
+ </p>
167
+ )}
168
+
169
+ <button
170
+ type="submit"
171
+ disabled={pending}
172
+ className={`${auth.btnPrimary} ${auth.btnShine}`}
173
+ data-testid="sign-in-button"
174
+ >
175
+ <span className="relative z-[1]">{pending ? "Signing in…" : "Sign in"}</span>
176
+ </button>
177
+ </form>
178
+ )
179
+ }
180
+
181
+ function OtpLoginForm({
182
+ countryCode,
183
+ OtpComponent,
184
+ }: {
185
+ countryCode: string
186
+ OtpComponent: OtpVerificationComponent
187
+ }) {
188
+ const [authMethod, setAuthMethod] = useState<"email_auth" | "phone_auth">("email_auth")
189
+ const [identifier, setIdentifier] = useState("")
190
+ const [otpToken, setOtpToken] = useState<string | null>(null)
191
+ const [otp, setOtp] = useState("")
192
+ const [showOtpModal, setShowOtpModal] = useState(false)
193
+ const [error, setError] = useState<string | null>(null)
194
+ const [pending, setPending] = useState(false)
195
+ const [isNewUser, setIsNewUser] = useState(false)
196
+ const [firstName, setFirstName] = useState("")
197
+ const [lastName, setLastName] = useState("")
198
+
199
+ async function handleSendOtp(e: React.FormEvent) {
200
+ e.preventDefault()
201
+ setPending(true)
202
+ setError(null)
203
+
204
+ try {
205
+ const res = await sendAuthOtp(
206
+ authMethod === "email_auth"
207
+ ? { email: identifier.trim(), type: "email_auth" }
208
+ : { phone: identifier.replace(/[^\d+]/g, ""), type: "phone_auth" }
209
+ )
210
+
211
+ if (!res.success) {
212
+ throw new Error(res.error)
213
+ }
214
+
215
+ setOtpToken(res.token ?? null)
216
+ setIsNewUser(res.isNewUser ?? false)
217
+ setOtp("")
218
+ setShowOtpModal(true)
219
+ } catch (err) {
220
+ if (isNextRedirect(err)) throw err
221
+ setError(safeErrorMessage(err, "Failed to send code"))
222
+ } finally {
223
+ setPending(false)
224
+ }
225
+ }
226
+
227
+ async function handleResendOtp() {
228
+ setPending(true)
229
+ setError(null)
230
+
231
+ try {
232
+ const res = await sendAuthOtp(
233
+ authMethod === "email_auth"
234
+ ? { email: identifier.trim(), type: "email_auth" }
235
+ : { phone: identifier.replace(/[^\d+]/g, ""), type: "phone_auth" }
236
+ )
237
+
238
+ if (!res.success) {
239
+ throw new Error(res.error)
240
+ }
241
+
242
+ setOtpToken(res.token ?? null)
243
+ setOtp("")
244
+ } catch (err) {
245
+ setError(safeErrorMessage(err, "Failed to resend code"))
246
+ } finally {
247
+ setPending(false)
248
+ }
249
+ }
250
+
251
+ async function handleVerifyOtp() {
252
+ if (!otp || otp.length < 6) return
253
+
254
+ setPending(true)
255
+ setError(null)
256
+
257
+ try {
258
+ if (isNewUser && (!firstName.trim() || !lastName.trim())) {
259
+ throw new Error("Please enter your first and last name to finish registration")
260
+ }
261
+
262
+ const result = await verifyAuthOtpAndLogin({
263
+ token: otpToken || "",
264
+ code: otp,
265
+ countryCode,
266
+ first_name: isNewUser ? firstName.trim() : undefined,
267
+ last_name: isNewUser ? lastName.trim() : undefined,
268
+ })
269
+ if (result && !result.success) {
270
+ throw new Error(result.error || "Verification failed")
271
+ }
272
+ } catch (err) {
273
+ if (isNextRedirect(err)) throw err
274
+ setError(safeErrorMessage(err, "Verification failed"))
275
+ setPending(false)
276
+ }
277
+ }
278
+
279
+ return (
280
+ <>
281
+ <form onSubmit={handleSendOtp} className="space-y-4">
282
+ <div className="flex flex-wrap gap-2">
283
+ <Chip active={authMethod === "email_auth"} onClick={() => setAuthMethod("email_auth")}>
284
+ Email OTP
285
+ </Chip>
286
+ <Chip active={authMethod === "phone_auth"} onClick={() => setAuthMethod("phone_auth")}>
287
+ Phone OTP
288
+ </Chip>
289
+ </div>
290
+
291
+ <AuthField
292
+ label={authMethod === "phone_auth" ? "Phone" : "Email"}
293
+ name="identifier"
294
+ type={authMethod === "phone_auth" ? "tel" : "email"}
295
+ value={identifier}
296
+ onChange={(e) => setIdentifier(e.target.value)}
297
+ autoComplete={authMethod === "phone_auth" ? "tel" : "email"}
298
+ placeholder={
299
+ authMethod === "phone_auth" ? "Enter your phone" : "Enter your email"
300
+ }
301
+ />
302
+
303
+ <p className="text-xs text-muted">
304
+ We will send a one-time code to sign in or create your account.
305
+ </p>
306
+
307
+ {error && !showOtpModal && (
308
+ <p className="text-sm text-brand-sale" role="alert">
309
+ {error}
310
+ </p>
311
+ )}
312
+
313
+ <button
314
+ type="submit"
315
+ disabled={pending || !identifier.trim()}
316
+ className={`${auth.btnPrimary} ${auth.btnShine}`}
317
+ >
318
+ <span className="relative z-[1]">
319
+ {pending ? "Sending…" : "Send verification code"}
320
+ </span>
321
+ </button>
322
+ </form>
323
+
324
+ <OtpComponent
325
+ open={showOtpModal}
326
+ preventClose
327
+ title={authMethod === "phone_auth" ? "Verify your phone" : "Verify your email"}
328
+ description={`Enter the 6-digit code sent to ${identifier || "your contact"}.`}
329
+ otp={otp}
330
+ onOtpChange={setOtp}
331
+ error={error}
332
+ pending={pending}
333
+ verifyLabel="Verify & sign in"
334
+ onVerify={handleVerifyOtp}
335
+ onResend={handleResendOtp}
336
+ extraContent={
337
+ isNewUser ? (
338
+ <div className="grid grid-cols-2 gap-3 mb-6">
339
+ <AuthField
340
+ label="First name"
341
+ name="first_name"
342
+ value={firstName}
343
+ onChange={(e) => setFirstName(e.target.value)}
344
+ required
345
+ />
346
+ <AuthField
347
+ label="Last name"
348
+ name="last_name"
349
+ value={lastName}
350
+ onChange={(e) => setLastName(e.target.value)}
351
+ required
352
+ />
353
+ </div>
354
+ ) : undefined
355
+ }
356
+ />
357
+ </>
358
+ )
359
+ }
360
+
361
+ export function AuthDivider({ label }: { label: string }) {
362
+ return (
363
+ <div className="relative">
364
+ <div className="absolute inset-0 flex items-center">
365
+ <div className="w-full border-t border-brand-primary/10" />
366
+ </div>
367
+ <div className="relative flex justify-center items-center gap-2 text-[10px] font-semibold uppercase tracking-[0.28em]">
368
+ <span className="px-2 bg-surface text-brand-accent">◆</span>
369
+ <span className="px-1 bg-surface text-muted">{label}</span>
370
+ <span className="px-2 bg-surface text-brand-accent">◆</span>
371
+ </div>
372
+ </div>
373
+ )
374
+ }
375
+
376
+ export function AuthField({
377
+ label,
378
+ name,
379
+ type = "text",
380
+ autoComplete,
381
+ value,
382
+ onChange,
383
+ placeholder,
384
+ required = true,
385
+ }: {
386
+ label: string
387
+ name?: string
388
+ type?: string
389
+ autoComplete?: string
390
+ value?: string
391
+ onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void
392
+ placeholder?: string
393
+ required?: boolean
394
+ }) {
395
+ return (
396
+ <label className="block">
397
+ <span className={`${auth.fieldLabel} mb-1.5 block`}>{label}</span>
398
+ <input
399
+ name={name}
400
+ type={type}
401
+ autoComplete={autoComplete}
402
+ value={value}
403
+ onChange={onChange}
404
+ placeholder={placeholder}
405
+ required={required}
406
+ className={auth.input}
407
+ />
408
+ </label>
409
+ )
410
+ }
411
+
412
+ export function Ornament() {
413
+ return (
414
+ <div className="flex items-center justify-center gap-2 mb-3" aria-hidden>
415
+ <span className="h-px w-6 bg-brand-accent/70" />
416
+ <span className="text-brand-accent text-[7px] leading-none">◆</span>
417
+ <span className="h-px w-6 bg-brand-accent/70" />
418
+ </div>
419
+ )
420
+ }
421
+
422
+ function Chip({
423
+ active,
424
+ onClick,
425
+ children,
426
+ }: {
427
+ active: boolean
428
+ onClick: () => void
429
+ children: React.ReactNode
430
+ }) {
431
+ return (
432
+ <button
433
+ type="button"
434
+ onClick={onClick}
435
+ className={active ? auth.chipActive : auth.chipIdle}
436
+ >
437
+ {children}
438
+ </button>
439
+ )
440
+ }
441
+
442
+
443
+ function GuestPackageIcon() {
444
+ return (
445
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
446
+ <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" strokeLinecap="round" strokeLinejoin="round" />
447
+ <polyline points="3.27 6.96 12 12.01 20.73 6.96" strokeLinecap="round" strokeLinejoin="round" />
448
+ <line x1="12" y1="22.08" x2="12" y2="12" strokeLinecap="round" />
449
+ </svg>
450
+ )
451
+ }
452
+
453
+ function EyeIcon() {
454
+ return (
455
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
456
+ <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
457
+ <circle cx="12" cy="12" r="3" />
458
+ </svg>
459
+ )
460
+ }
461
+
462
+ function EyeOffIcon() {
463
+ return (
464
+ <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden>
465
+ <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" />
466
+ <line x1="1" y1="1" x2="23" y2="23" />
467
+ </svg>
468
+ )
469
+ }