@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.
- package/package.json +58 -54
- package/src/account-addresses.tsx +307 -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/address-actions.ts +74 -0
- package/src/auth-server.ts +442 -442
- package/src/card-expiry.test.ts +71 -0
- package/src/card-expiry.ts +29 -0
- package/src/commerce-auth-otp-modal.css +125 -125
- package/src/commerce-auth-otp-modal.tsx +121 -121
- package/src/default-address.test.ts +143 -0
- package/src/default-address.ts +51 -0
- package/src/forgot-password-form.tsx +109 -109
- package/src/google-auth-section.tsx +22 -22
- package/src/google-sign-in-section.tsx +67 -67
- 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 +159 -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 +103 -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/otp-unified-form.tsx
CHANGED
|
@@ -1,333 +1,333 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
-
import { useEffect, useState } from "react"
|
|
5
|
-
import { SplitPhoneInput } from "@pradip1995/commerce-core/components/split-phone-input"
|
|
6
|
-
import {
|
|
7
|
-
formatPhoneDisplay,
|
|
8
|
-
isValidPhoneParts,
|
|
9
|
-
parsePhoneParts,
|
|
10
|
-
toE164Phone,
|
|
11
|
-
} from "@pradip1995/commerce-core/util/phone"
|
|
12
|
-
import { sendAuthOtp, verifyAuthOtpAndLogin } from "./auth-server"
|
|
13
|
-
import GoogleAuthSection from "./google-auth-section"
|
|
14
|
-
import OtpInput from "./otp-input"
|
|
15
|
-
import { auth } from "./account-theme"
|
|
16
|
-
import { AuthDivider, AuthField, Ornament } from "./login-form"
|
|
17
|
-
|
|
18
|
-
type AuthMethod = "email_auth" | "phone_auth"
|
|
19
|
-
type OtpStep = "identifier" | "verify"
|
|
20
|
-
|
|
21
|
-
const RESEND_COOLDOWN_SEC = 30
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Sahsha-style single-screen OTP auth: Google + Mobile/Email OTP.
|
|
25
|
-
* New users finish signup in the same verify step (first name).
|
|
26
|
-
*/
|
|
27
|
-
export default function OtpUnifiedForm({
|
|
28
|
-
countryCode,
|
|
29
|
-
onTrackGuest,
|
|
30
|
-
}: {
|
|
31
|
-
countryCode: string
|
|
32
|
-
onTrackGuest?: () => void
|
|
33
|
-
}) {
|
|
34
|
-
const [authMethod, setAuthMethod] = useState<AuthMethod>("phone_auth")
|
|
35
|
-
const [identifier, setIdentifier] = useState("")
|
|
36
|
-
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
37
|
-
const [otp, setOtp] = useState("")
|
|
38
|
-
const [step, setStep] = useState<OtpStep>("identifier")
|
|
39
|
-
const [error, setError] = useState<string | null>(null)
|
|
40
|
-
const [sending, setSending] = useState(false)
|
|
41
|
-
const [verifying, setVerifying] = useState(false)
|
|
42
|
-
const [isNewUser, setIsNewUser] = useState(false)
|
|
43
|
-
const [firstName, setFirstName] = useState("")
|
|
44
|
-
const [resendIn, setResendIn] = useState(0)
|
|
45
|
-
|
|
46
|
-
useEffect(() => {
|
|
47
|
-
if (resendIn <= 0) return
|
|
48
|
-
const timer = window.setTimeout(() => setResendIn((s) => s - 1), 1000)
|
|
49
|
-
return () => window.clearTimeout(timer)
|
|
50
|
-
}, [resendIn])
|
|
51
|
-
|
|
52
|
-
const phoneParts = parsePhoneParts(identifier)
|
|
53
|
-
const phoneReady =
|
|
54
|
-
authMethod === "phone_auth" &&
|
|
55
|
-
isValidPhoneParts(phoneParts.dialCode, phoneParts.localNumber)
|
|
56
|
-
const identifierReady =
|
|
57
|
-
authMethod === "email_auth" ? Boolean(identifier.trim()) : phoneReady
|
|
58
|
-
|
|
59
|
-
async function handleSendOtp(options?: { resend?: boolean }) {
|
|
60
|
-
if (!identifierReady || sending) return
|
|
61
|
-
setSending(true)
|
|
62
|
-
setError(null)
|
|
63
|
-
|
|
64
|
-
// Advance immediately so the user is not stuck on "Sending…" during SMTP/SMS.
|
|
65
|
-
if (!options?.resend) {
|
|
66
|
-
setOtp("")
|
|
67
|
-
setFirstName("")
|
|
68
|
-
setOtpToken(null)
|
|
69
|
-
setStep("verify")
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
try {
|
|
73
|
-
const res = await sendAuthOtp(
|
|
74
|
-
authMethod === "email_auth"
|
|
75
|
-
? { email: identifier.trim(), type: "email_auth" }
|
|
76
|
-
: { phone: toE164Phone(identifier), type: "phone_auth" }
|
|
77
|
-
)
|
|
78
|
-
if (!res.success) throw new Error(res.error)
|
|
79
|
-
setOtpToken(res.token ?? null)
|
|
80
|
-
setIsNewUser(res.isNewUser ?? false)
|
|
81
|
-
setOtp("")
|
|
82
|
-
setResendIn(RESEND_COOLDOWN_SEC)
|
|
83
|
-
} catch (err) {
|
|
84
|
-
if (isNextRedirect(err)) throw err
|
|
85
|
-
setError(safeErrorMessage(err, "Failed to send code"))
|
|
86
|
-
if (!options?.resend) {
|
|
87
|
-
setStep("identifier")
|
|
88
|
-
}
|
|
89
|
-
} finally {
|
|
90
|
-
setSending(false)
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function handleVerify() {
|
|
95
|
-
if (!otp || otp.length < 6 || !otpToken || verifying || sending) return
|
|
96
|
-
setVerifying(true)
|
|
97
|
-
setError(null)
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
if (isNewUser && !firstName.trim()) {
|
|
101
|
-
throw new Error("Please enter your first name to finish registration")
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const result = await verifyAuthOtpAndLogin({
|
|
105
|
-
token: otpToken,
|
|
106
|
-
code: otp,
|
|
107
|
-
countryCode,
|
|
108
|
-
first_name: isNewUser ? firstName.trim() : undefined,
|
|
109
|
-
last_name: isNewUser ? "." : undefined,
|
|
110
|
-
})
|
|
111
|
-
if (result && !result.success) {
|
|
112
|
-
throw new Error(result.error || "Verification failed")
|
|
113
|
-
}
|
|
114
|
-
} catch (err) {
|
|
115
|
-
if (isNextRedirect(err)) throw err
|
|
116
|
-
setError(safeErrorMessage(err, "Verification failed"))
|
|
117
|
-
setVerifying(false)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function resetToIdentifier() {
|
|
122
|
-
setStep("identifier")
|
|
123
|
-
setOtp("")
|
|
124
|
-
setOtpToken(null)
|
|
125
|
-
setError(null)
|
|
126
|
-
setIsNewUser(false)
|
|
127
|
-
setFirstName("")
|
|
128
|
-
setSending(false)
|
|
129
|
-
setVerifying(false)
|
|
130
|
-
setResendIn(0)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const identifierDisplay =
|
|
134
|
-
authMethod === "phone_auth" ? formatPhoneDisplay(identifier) || identifier : identifier
|
|
135
|
-
const channelLabel = authMethod === "email_auth" ? "email" : "number"
|
|
136
|
-
const verifyHint = sending
|
|
137
|
-
? `Sending code to ${identifierDisplay}…`
|
|
138
|
-
: `Enter the 6-digit code sent to ${identifierDisplay}`
|
|
139
|
-
|
|
140
|
-
return (
|
|
141
|
-
<div className="w-full flex flex-col space-y-5" data-testid="login-page">
|
|
142
|
-
<div className="text-center lg:hidden">
|
|
143
|
-
<Ornament />
|
|
144
|
-
<h2 className={auth.mobileTitle}>Sign in</h2>
|
|
145
|
-
<p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
|
|
146
|
-
Select your login mode to continue
|
|
147
|
-
</p>
|
|
148
|
-
</div>
|
|
149
|
-
|
|
150
|
-
{step === "identifier" ? (
|
|
151
|
-
<>
|
|
152
|
-
<GoogleAuthSection countryCode={countryCode} />
|
|
153
|
-
<AuthDivider label="Or continue with email / mobile" />
|
|
154
|
-
|
|
155
|
-
<div className="flex gap-2 p-1 bg-surface-muted/80">
|
|
156
|
-
<ModeButton
|
|
157
|
-
active={authMethod === "phone_auth"}
|
|
158
|
-
onClick={() => {
|
|
159
|
-
setAuthMethod("phone_auth")
|
|
160
|
-
setIdentifier("")
|
|
161
|
-
setError(null)
|
|
162
|
-
}}
|
|
163
|
-
>
|
|
164
|
-
Mobile
|
|
165
|
-
</ModeButton>
|
|
166
|
-
<ModeButton
|
|
167
|
-
active={authMethod === "email_auth"}
|
|
168
|
-
onClick={() => {
|
|
169
|
-
setAuthMethod("email_auth")
|
|
170
|
-
setIdentifier("")
|
|
171
|
-
setError(null)
|
|
172
|
-
}}
|
|
173
|
-
>
|
|
174
|
-
Email
|
|
175
|
-
</ModeButton>
|
|
176
|
-
</div>
|
|
177
|
-
|
|
178
|
-
{authMethod === "phone_auth" ? (
|
|
179
|
-
<SplitPhoneInput
|
|
180
|
-
value={identifier}
|
|
181
|
-
onChange={setIdentifier}
|
|
182
|
-
label="Mobile number"
|
|
183
|
-
numberInputId="account-login-phone"
|
|
184
|
-
data-testid="account-login-phone"
|
|
185
|
-
/>
|
|
186
|
-
) : (
|
|
187
|
-
<AuthField
|
|
188
|
-
label="Email"
|
|
189
|
-
name="identifier"
|
|
190
|
-
type="email"
|
|
191
|
-
value={identifier}
|
|
192
|
-
onChange={(e) => setIdentifier(e.target.value)}
|
|
193
|
-
autoComplete="email"
|
|
194
|
-
placeholder="Enter your email"
|
|
195
|
-
/>
|
|
196
|
-
)}
|
|
197
|
-
|
|
198
|
-
{error && (
|
|
199
|
-
<p className="text-sm text-brand-sale" role="alert">
|
|
200
|
-
{error}
|
|
201
|
-
</p>
|
|
202
|
-
)}
|
|
203
|
-
|
|
204
|
-
<button
|
|
205
|
-
type="button"
|
|
206
|
-
disabled={sending || !identifierReady}
|
|
207
|
-
onClick={() => void handleSendOtp()}
|
|
208
|
-
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
209
|
-
>
|
|
210
|
-
<span className="relative z-[1]">{sending ? "Continue…" : "Continue"}</span>
|
|
211
|
-
</button>
|
|
212
|
-
</>
|
|
213
|
-
) : (
|
|
214
|
-
<div className="space-y-5">
|
|
215
|
-
<div className="text-center">
|
|
216
|
-
<h3 className="font-heading text-lg font-bold text-heading uppercase tracking-[0.08em]">
|
|
217
|
-
Verify OTP
|
|
218
|
-
</h3>
|
|
219
|
-
<p className="mt-2 text-sm text-muted" aria-live="polite">
|
|
220
|
-
{sending ? (
|
|
221
|
-
verifyHint
|
|
222
|
-
) : (
|
|
223
|
-
<>
|
|
224
|
-
Enter the 6-digit code sent to{" "}
|
|
225
|
-
<span className="text-heading font-medium">{identifierDisplay}</span>
|
|
226
|
-
</>
|
|
227
|
-
)}
|
|
228
|
-
</p>
|
|
229
|
-
</div>
|
|
230
|
-
|
|
231
|
-
{isNewUser && (
|
|
232
|
-
<AuthField
|
|
233
|
-
label="First name"
|
|
234
|
-
name="first_name"
|
|
235
|
-
value={firstName}
|
|
236
|
-
onChange={(e) => setFirstName(e.target.value)}
|
|
237
|
-
placeholder="Your first name"
|
|
238
|
-
required
|
|
239
|
-
/>
|
|
240
|
-
)}
|
|
241
|
-
|
|
242
|
-
<OtpInput
|
|
243
|
-
value={otp}
|
|
244
|
-
onChange={setOtp}
|
|
245
|
-
autoFocus={!isNewUser && !sending && Boolean(otpToken)}
|
|
246
|
-
/>
|
|
247
|
-
|
|
248
|
-
{error && (
|
|
249
|
-
<p className="text-sm text-brand-sale text-center" role="alert">
|
|
250
|
-
{error}
|
|
251
|
-
</p>
|
|
252
|
-
)}
|
|
253
|
-
|
|
254
|
-
<button
|
|
255
|
-
type="button"
|
|
256
|
-
disabled={
|
|
257
|
-
sending ||
|
|
258
|
-
verifying ||
|
|
259
|
-
!otpToken ||
|
|
260
|
-
otp.length < 6 ||
|
|
261
|
-
(isNewUser && !firstName.trim())
|
|
262
|
-
}
|
|
263
|
-
onClick={() => void handleVerify()}
|
|
264
|
-
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
265
|
-
>
|
|
266
|
-
<span className="relative z-[1]">
|
|
267
|
-
{sending
|
|
268
|
-
? "Waiting for code…"
|
|
269
|
-
: verifying
|
|
270
|
-
? "Verifying…"
|
|
271
|
-
: isNewUser
|
|
272
|
-
? "Verify & create account"
|
|
273
|
-
: "Verify & sign in"}
|
|
274
|
-
</span>
|
|
275
|
-
</button>
|
|
276
|
-
|
|
277
|
-
<div className="flex items-center justify-between gap-3 text-xs">
|
|
278
|
-
<button
|
|
279
|
-
type="button"
|
|
280
|
-
onClick={resetToIdentifier}
|
|
281
|
-
disabled={verifying}
|
|
282
|
-
className="text-muted hover:text-brand-accent transition-colors disabled:opacity-50"
|
|
283
|
-
>
|
|
284
|
-
Change {channelLabel}
|
|
285
|
-
</button>
|
|
286
|
-
<button
|
|
287
|
-
type="button"
|
|
288
|
-
disabled={sending || verifying || resendIn > 0}
|
|
289
|
-
onClick={() => void handleSendOtp({ resend: true })}
|
|
290
|
-
className="text-muted hover:text-brand-accent transition-colors disabled:opacity-50"
|
|
291
|
-
>
|
|
292
|
-
{sending
|
|
293
|
-
? "Resending…"
|
|
294
|
-
: resendIn > 0
|
|
295
|
-
? `Resend in ${resendIn}s`
|
|
296
|
-
: "Resend OTP"}
|
|
297
|
-
</button>
|
|
298
|
-
</div>
|
|
299
|
-
</div>
|
|
300
|
-
)}
|
|
301
|
-
|
|
302
|
-
{onTrackGuest && step === "identifier" && (
|
|
303
|
-
<div className="pt-4 border-t border-brand-primary/10">
|
|
304
|
-
<button
|
|
305
|
-
type="button"
|
|
306
|
-
onClick={onTrackGuest}
|
|
307
|
-
className={auth.guestCta}
|
|
308
|
-
data-testid="track-guest-order-button"
|
|
309
|
-
>
|
|
310
|
-
Track guest order
|
|
311
|
-
</button>
|
|
312
|
-
</div>
|
|
313
|
-
)}
|
|
314
|
-
</div>
|
|
315
|
-
)
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function ModeButton({
|
|
319
|
-
active,
|
|
320
|
-
onClick,
|
|
321
|
-
children,
|
|
322
|
-
}: {
|
|
323
|
-
active: boolean
|
|
324
|
-
onClick: () => void
|
|
325
|
-
children: React.ReactNode
|
|
326
|
-
}) {
|
|
327
|
-
return (
|
|
328
|
-
<button type="button" onClick={onClick} className={active ? auth.modeActive : auth.modeIdle}>
|
|
329
|
-
{children}
|
|
330
|
-
</button>
|
|
331
|
-
)
|
|
332
|
-
}
|
|
333
|
-
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
+
import { useEffect, useState } from "react"
|
|
5
|
+
import { SplitPhoneInput } from "@pradip1995/commerce-core/components/split-phone-input"
|
|
6
|
+
import {
|
|
7
|
+
formatPhoneDisplay,
|
|
8
|
+
isValidPhoneParts,
|
|
9
|
+
parsePhoneParts,
|
|
10
|
+
toE164Phone,
|
|
11
|
+
} from "@pradip1995/commerce-core/util/phone"
|
|
12
|
+
import { sendAuthOtp, verifyAuthOtpAndLogin } from "./auth-server"
|
|
13
|
+
import GoogleAuthSection from "./google-auth-section"
|
|
14
|
+
import OtpInput from "./otp-input"
|
|
15
|
+
import { auth } from "./account-theme"
|
|
16
|
+
import { AuthDivider, AuthField, Ornament } from "./login-form"
|
|
17
|
+
|
|
18
|
+
type AuthMethod = "email_auth" | "phone_auth"
|
|
19
|
+
type OtpStep = "identifier" | "verify"
|
|
20
|
+
|
|
21
|
+
const RESEND_COOLDOWN_SEC = 30
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sahsha-style single-screen OTP auth: Google + Mobile/Email OTP.
|
|
25
|
+
* New users finish signup in the same verify step (first name).
|
|
26
|
+
*/
|
|
27
|
+
export default function OtpUnifiedForm({
|
|
28
|
+
countryCode,
|
|
29
|
+
onTrackGuest,
|
|
30
|
+
}: {
|
|
31
|
+
countryCode: string
|
|
32
|
+
onTrackGuest?: () => void
|
|
33
|
+
}) {
|
|
34
|
+
const [authMethod, setAuthMethod] = useState<AuthMethod>("phone_auth")
|
|
35
|
+
const [identifier, setIdentifier] = useState("")
|
|
36
|
+
const [otpToken, setOtpToken] = useState<string | null>(null)
|
|
37
|
+
const [otp, setOtp] = useState("")
|
|
38
|
+
const [step, setStep] = useState<OtpStep>("identifier")
|
|
39
|
+
const [error, setError] = useState<string | null>(null)
|
|
40
|
+
const [sending, setSending] = useState(false)
|
|
41
|
+
const [verifying, setVerifying] = useState(false)
|
|
42
|
+
const [isNewUser, setIsNewUser] = useState(false)
|
|
43
|
+
const [firstName, setFirstName] = useState("")
|
|
44
|
+
const [resendIn, setResendIn] = useState(0)
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (resendIn <= 0) return
|
|
48
|
+
const timer = window.setTimeout(() => setResendIn((s) => s - 1), 1000)
|
|
49
|
+
return () => window.clearTimeout(timer)
|
|
50
|
+
}, [resendIn])
|
|
51
|
+
|
|
52
|
+
const phoneParts = parsePhoneParts(identifier)
|
|
53
|
+
const phoneReady =
|
|
54
|
+
authMethod === "phone_auth" &&
|
|
55
|
+
isValidPhoneParts(phoneParts.dialCode, phoneParts.localNumber)
|
|
56
|
+
const identifierReady =
|
|
57
|
+
authMethod === "email_auth" ? Boolean(identifier.trim()) : phoneReady
|
|
58
|
+
|
|
59
|
+
async function handleSendOtp(options?: { resend?: boolean }) {
|
|
60
|
+
if (!identifierReady || sending) return
|
|
61
|
+
setSending(true)
|
|
62
|
+
setError(null)
|
|
63
|
+
|
|
64
|
+
// Advance immediately so the user is not stuck on "Sending…" during SMTP/SMS.
|
|
65
|
+
if (!options?.resend) {
|
|
66
|
+
setOtp("")
|
|
67
|
+
setFirstName("")
|
|
68
|
+
setOtpToken(null)
|
|
69
|
+
setStep("verify")
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const res = await sendAuthOtp(
|
|
74
|
+
authMethod === "email_auth"
|
|
75
|
+
? { email: identifier.trim(), type: "email_auth" }
|
|
76
|
+
: { phone: toE164Phone(identifier), type: "phone_auth" }
|
|
77
|
+
)
|
|
78
|
+
if (!res.success) throw new Error(res.error)
|
|
79
|
+
setOtpToken(res.token ?? null)
|
|
80
|
+
setIsNewUser(res.isNewUser ?? false)
|
|
81
|
+
setOtp("")
|
|
82
|
+
setResendIn(RESEND_COOLDOWN_SEC)
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (isNextRedirect(err)) throw err
|
|
85
|
+
setError(safeErrorMessage(err, "Failed to send code"))
|
|
86
|
+
if (!options?.resend) {
|
|
87
|
+
setStep("identifier")
|
|
88
|
+
}
|
|
89
|
+
} finally {
|
|
90
|
+
setSending(false)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function handleVerify() {
|
|
95
|
+
if (!otp || otp.length < 6 || !otpToken || verifying || sending) return
|
|
96
|
+
setVerifying(true)
|
|
97
|
+
setError(null)
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
if (isNewUser && !firstName.trim()) {
|
|
101
|
+
throw new Error("Please enter your first name to finish registration")
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const result = await verifyAuthOtpAndLogin({
|
|
105
|
+
token: otpToken,
|
|
106
|
+
code: otp,
|
|
107
|
+
countryCode,
|
|
108
|
+
first_name: isNewUser ? firstName.trim() : undefined,
|
|
109
|
+
last_name: isNewUser ? "." : undefined,
|
|
110
|
+
})
|
|
111
|
+
if (result && !result.success) {
|
|
112
|
+
throw new Error(result.error || "Verification failed")
|
|
113
|
+
}
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (isNextRedirect(err)) throw err
|
|
116
|
+
setError(safeErrorMessage(err, "Verification failed"))
|
|
117
|
+
setVerifying(false)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function resetToIdentifier() {
|
|
122
|
+
setStep("identifier")
|
|
123
|
+
setOtp("")
|
|
124
|
+
setOtpToken(null)
|
|
125
|
+
setError(null)
|
|
126
|
+
setIsNewUser(false)
|
|
127
|
+
setFirstName("")
|
|
128
|
+
setSending(false)
|
|
129
|
+
setVerifying(false)
|
|
130
|
+
setResendIn(0)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const identifierDisplay =
|
|
134
|
+
authMethod === "phone_auth" ? formatPhoneDisplay(identifier) || identifier : identifier
|
|
135
|
+
const channelLabel = authMethod === "email_auth" ? "email" : "number"
|
|
136
|
+
const verifyHint = sending
|
|
137
|
+
? `Sending code to ${identifierDisplay}…`
|
|
138
|
+
: `Enter the 6-digit code sent to ${identifierDisplay}`
|
|
139
|
+
|
|
140
|
+
return (
|
|
141
|
+
<div className="w-full flex flex-col space-y-5" data-testid="login-page">
|
|
142
|
+
<div className="text-center lg:hidden">
|
|
143
|
+
<Ornament />
|
|
144
|
+
<h2 className={auth.mobileTitle}>Sign in</h2>
|
|
145
|
+
<p className="mt-2 text-[10px] text-muted uppercase tracking-[0.24em]">
|
|
146
|
+
Select your login mode to continue
|
|
147
|
+
</p>
|
|
148
|
+
</div>
|
|
149
|
+
|
|
150
|
+
{step === "identifier" ? (
|
|
151
|
+
<>
|
|
152
|
+
<GoogleAuthSection countryCode={countryCode} />
|
|
153
|
+
<AuthDivider label="Or continue with email / mobile" />
|
|
154
|
+
|
|
155
|
+
<div className="flex gap-2 p-1 bg-surface-muted/80">
|
|
156
|
+
<ModeButton
|
|
157
|
+
active={authMethod === "phone_auth"}
|
|
158
|
+
onClick={() => {
|
|
159
|
+
setAuthMethod("phone_auth")
|
|
160
|
+
setIdentifier("")
|
|
161
|
+
setError(null)
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
Mobile
|
|
165
|
+
</ModeButton>
|
|
166
|
+
<ModeButton
|
|
167
|
+
active={authMethod === "email_auth"}
|
|
168
|
+
onClick={() => {
|
|
169
|
+
setAuthMethod("email_auth")
|
|
170
|
+
setIdentifier("")
|
|
171
|
+
setError(null)
|
|
172
|
+
}}
|
|
173
|
+
>
|
|
174
|
+
Email
|
|
175
|
+
</ModeButton>
|
|
176
|
+
</div>
|
|
177
|
+
|
|
178
|
+
{authMethod === "phone_auth" ? (
|
|
179
|
+
<SplitPhoneInput
|
|
180
|
+
value={identifier}
|
|
181
|
+
onChange={setIdentifier}
|
|
182
|
+
label="Mobile number"
|
|
183
|
+
numberInputId="account-login-phone"
|
|
184
|
+
data-testid="account-login-phone"
|
|
185
|
+
/>
|
|
186
|
+
) : (
|
|
187
|
+
<AuthField
|
|
188
|
+
label="Email"
|
|
189
|
+
name="identifier"
|
|
190
|
+
type="email"
|
|
191
|
+
value={identifier}
|
|
192
|
+
onChange={(e) => setIdentifier(e.target.value)}
|
|
193
|
+
autoComplete="email"
|
|
194
|
+
placeholder="Enter your email"
|
|
195
|
+
/>
|
|
196
|
+
)}
|
|
197
|
+
|
|
198
|
+
{error && (
|
|
199
|
+
<p className="text-sm text-brand-sale" role="alert">
|
|
200
|
+
{error}
|
|
201
|
+
</p>
|
|
202
|
+
)}
|
|
203
|
+
|
|
204
|
+
<button
|
|
205
|
+
type="button"
|
|
206
|
+
disabled={sending || !identifierReady}
|
|
207
|
+
onClick={() => void handleSendOtp()}
|
|
208
|
+
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
209
|
+
>
|
|
210
|
+
<span className="relative z-[1]">{sending ? "Continue…" : "Continue"}</span>
|
|
211
|
+
</button>
|
|
212
|
+
</>
|
|
213
|
+
) : (
|
|
214
|
+
<div className="space-y-5">
|
|
215
|
+
<div className="text-center">
|
|
216
|
+
<h3 className="font-heading text-lg font-bold text-heading uppercase tracking-[0.08em]">
|
|
217
|
+
Verify OTP
|
|
218
|
+
</h3>
|
|
219
|
+
<p className="mt-2 text-sm text-muted" aria-live="polite">
|
|
220
|
+
{sending ? (
|
|
221
|
+
verifyHint
|
|
222
|
+
) : (
|
|
223
|
+
<>
|
|
224
|
+
Enter the 6-digit code sent to{" "}
|
|
225
|
+
<span className="text-heading font-medium">{identifierDisplay}</span>
|
|
226
|
+
</>
|
|
227
|
+
)}
|
|
228
|
+
</p>
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
{isNewUser && (
|
|
232
|
+
<AuthField
|
|
233
|
+
label="First name"
|
|
234
|
+
name="first_name"
|
|
235
|
+
value={firstName}
|
|
236
|
+
onChange={(e) => setFirstName(e.target.value)}
|
|
237
|
+
placeholder="Your first name"
|
|
238
|
+
required
|
|
239
|
+
/>
|
|
240
|
+
)}
|
|
241
|
+
|
|
242
|
+
<OtpInput
|
|
243
|
+
value={otp}
|
|
244
|
+
onChange={setOtp}
|
|
245
|
+
autoFocus={!isNewUser && !sending && Boolean(otpToken)}
|
|
246
|
+
/>
|
|
247
|
+
|
|
248
|
+
{error && (
|
|
249
|
+
<p className="text-sm text-brand-sale text-center" role="alert">
|
|
250
|
+
{error}
|
|
251
|
+
</p>
|
|
252
|
+
)}
|
|
253
|
+
|
|
254
|
+
<button
|
|
255
|
+
type="button"
|
|
256
|
+
disabled={
|
|
257
|
+
sending ||
|
|
258
|
+
verifying ||
|
|
259
|
+
!otpToken ||
|
|
260
|
+
otp.length < 6 ||
|
|
261
|
+
(isNewUser && !firstName.trim())
|
|
262
|
+
}
|
|
263
|
+
onClick={() => void handleVerify()}
|
|
264
|
+
className={`${auth.btnPrimary} ${auth.btnShine}`}
|
|
265
|
+
>
|
|
266
|
+
<span className="relative z-[1]">
|
|
267
|
+
{sending
|
|
268
|
+
? "Waiting for code…"
|
|
269
|
+
: verifying
|
|
270
|
+
? "Verifying…"
|
|
271
|
+
: isNewUser
|
|
272
|
+
? "Verify & create account"
|
|
273
|
+
: "Verify & sign in"}
|
|
274
|
+
</span>
|
|
275
|
+
</button>
|
|
276
|
+
|
|
277
|
+
<div className="flex items-center justify-between gap-3 text-xs">
|
|
278
|
+
<button
|
|
279
|
+
type="button"
|
|
280
|
+
onClick={resetToIdentifier}
|
|
281
|
+
disabled={verifying}
|
|
282
|
+
className="text-muted hover:text-brand-accent transition-colors disabled:opacity-50"
|
|
283
|
+
>
|
|
284
|
+
Change {channelLabel}
|
|
285
|
+
</button>
|
|
286
|
+
<button
|
|
287
|
+
type="button"
|
|
288
|
+
disabled={sending || verifying || resendIn > 0}
|
|
289
|
+
onClick={() => void handleSendOtp({ resend: true })}
|
|
290
|
+
className="text-muted hover:text-brand-accent transition-colors disabled:opacity-50"
|
|
291
|
+
>
|
|
292
|
+
{sending
|
|
293
|
+
? "Resending…"
|
|
294
|
+
: resendIn > 0
|
|
295
|
+
? `Resend in ${resendIn}s`
|
|
296
|
+
: "Resend OTP"}
|
|
297
|
+
</button>
|
|
298
|
+
</div>
|
|
299
|
+
</div>
|
|
300
|
+
)}
|
|
301
|
+
|
|
302
|
+
{onTrackGuest && step === "identifier" && (
|
|
303
|
+
<div className="pt-4 border-t border-brand-primary/10">
|
|
304
|
+
<button
|
|
305
|
+
type="button"
|
|
306
|
+
onClick={onTrackGuest}
|
|
307
|
+
className={auth.guestCta}
|
|
308
|
+
data-testid="track-guest-order-button"
|
|
309
|
+
>
|
|
310
|
+
Track guest order
|
|
311
|
+
</button>
|
|
312
|
+
</div>
|
|
313
|
+
)}
|
|
314
|
+
</div>
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function ModeButton({
|
|
319
|
+
active,
|
|
320
|
+
onClick,
|
|
321
|
+
children,
|
|
322
|
+
}: {
|
|
323
|
+
active: boolean
|
|
324
|
+
onClick: () => void
|
|
325
|
+
children: React.ReactNode
|
|
326
|
+
}) {
|
|
327
|
+
return (
|
|
328
|
+
<button type="button" onClick={onClick} className={active ? auth.modeActive : auth.modeIdle}>
|
|
329
|
+
{children}
|
|
330
|
+
</button>
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
|