@pradip1995/segment-animation-login 0.1.1

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.
@@ -0,0 +1,358 @@
1
+ "use client"
2
+
3
+ import { useEffect, useState } from "react"
4
+ import GoogleAuthSection from "@pradip1995/segment-login-template/google-auth-section"
5
+ import OtpInput from "@pradip1995/segment-login-template/otp-input"
6
+ import {
7
+ sendAuthOtp,
8
+ verifyAuthOtpAndLogin,
9
+ } from "@pradip1995/segment-login-template/auth-server"
10
+ import { SplitPhoneInput } from "@pradip1995/commerce-core/components/split-phone-input"
11
+ import {
12
+ formatPhoneDisplay,
13
+ isValidPhoneParts,
14
+ parsePhoneParts,
15
+ toE164Phone,
16
+ } from "@pradip1995/commerce-core/util/phone"
17
+ import {
18
+ isNextRedirect,
19
+ safeErrorMessage,
20
+ } from "@pradip1995/segment-primitives/is-next-redirect"
21
+ import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
22
+
23
+ type AuthMethod = "email_auth" | "phone_auth"
24
+ type OtpStep = "identifier" | "verify"
25
+
26
+ const RESEND_COOLDOWN_SEC = 30
27
+
28
+ export default function AnimationLoginForm({
29
+ countryCode,
30
+ title = "Sign in",
31
+ subtitle = "Select your login mode to continue",
32
+ shopName = "Lumière",
33
+ privacyHref = "/privacy-policy",
34
+ termsHref = "/terms-of-use",
35
+ onTrackGuest,
36
+ }: {
37
+ countryCode: string
38
+ title?: string
39
+ subtitle?: string
40
+ shopName?: string
41
+ privacyHref?: string
42
+ termsHref?: string
43
+ onTrackGuest?: () => void
44
+ }) {
45
+ const [authMethod, setAuthMethod] = useState<AuthMethod>("phone_auth")
46
+ const [phone, setPhone] = useState("")
47
+ const [email, setEmail] = useState("")
48
+ const identifier = authMethod === "phone_auth" ? phone : email
49
+ const [otpToken, setOtpToken] = useState<string | null>(null)
50
+ const [otp, setOtp] = useState("")
51
+ const [step, setStep] = useState<OtpStep>("identifier")
52
+ const [error, setError] = useState<string | null>(null)
53
+ const [sending, setSending] = useState(false)
54
+ const [verifying, setVerifying] = useState(false)
55
+ const [isNewUser, setIsNewUser] = useState(false)
56
+ const [firstName, setFirstName] = useState("")
57
+ const [resendIn, setResendIn] = useState(0)
58
+
59
+ useEffect(() => {
60
+ if (resendIn <= 0) return
61
+ const timer = window.setTimeout(() => setResendIn((s) => s - 1), 1000)
62
+ return () => window.clearTimeout(timer)
63
+ }, [resendIn])
64
+
65
+ const phoneParts = parsePhoneParts(identifier)
66
+ const phoneReady =
67
+ authMethod === "phone_auth" &&
68
+ isValidPhoneParts(phoneParts.dialCode, phoneParts.localNumber)
69
+ const identifierReady =
70
+ authMethod === "email_auth" ? Boolean(identifier.trim()) : phoneReady
71
+
72
+ async function handleSendOtp(options?: { resend?: boolean }) {
73
+ if (!identifierReady || sending) return
74
+ setSending(true)
75
+ setError(null)
76
+
77
+ if (!options?.resend) {
78
+ setOtp("")
79
+ setFirstName("")
80
+ setOtpToken(null)
81
+ setStep("verify")
82
+ }
83
+
84
+ try {
85
+ const res = await sendAuthOtp(
86
+ authMethod === "email_auth"
87
+ ? { email: identifier.trim(), type: "email_auth" }
88
+ : { phone: toE164Phone(identifier), type: "phone_auth" }
89
+ )
90
+ if (!res.success) throw new Error(res.error)
91
+ setOtpToken(res.token ?? null)
92
+ setIsNewUser(res.isNewUser ?? false)
93
+ setOtp("")
94
+ setResendIn(RESEND_COOLDOWN_SEC)
95
+ } catch (err) {
96
+ if (isNextRedirect(err)) throw err
97
+ setError(safeErrorMessage(err, "Failed to send code"))
98
+ if (!options?.resend) {
99
+ setStep("identifier")
100
+ }
101
+ } finally {
102
+ setSending(false)
103
+ }
104
+ }
105
+
106
+ async function handleVerify() {
107
+ if (!otp || otp.length < 6 || !otpToken || verifying || sending) return
108
+ setVerifying(true)
109
+ setError(null)
110
+
111
+ try {
112
+ if (isNewUser && !firstName.trim()) {
113
+ throw new Error("Please enter your first name to finish registration")
114
+ }
115
+
116
+ const result = await verifyAuthOtpAndLogin({
117
+ token: otpToken,
118
+ code: otp,
119
+ countryCode,
120
+ first_name: isNewUser ? firstName.trim() : undefined,
121
+ last_name: isNewUser ? "." : undefined,
122
+ })
123
+ if (result && !result.success) {
124
+ throw new Error(result.error || "Verification failed")
125
+ }
126
+ } catch (err) {
127
+ if (isNextRedirect(err)) throw err
128
+ setError(safeErrorMessage(err, "Verification failed"))
129
+ setVerifying(false)
130
+ }
131
+ }
132
+
133
+ function resetToIdentifier() {
134
+ setStep("identifier")
135
+ setOtp("")
136
+ setOtpToken(null)
137
+ setError(null)
138
+ setIsNewUser(false)
139
+ setFirstName("")
140
+ setSending(false)
141
+ setVerifying(false)
142
+ setResendIn(0)
143
+ }
144
+
145
+ const identifierDisplay =
146
+ authMethod === "phone_auth" ? formatPhoneDisplay(identifier) || identifier : identifier
147
+ const channelLabel = authMethod === "email_auth" ? "email" : "number"
148
+
149
+ return (
150
+ <div className="anim-login__form-inner" data-testid="login-page">
151
+ {step === "identifier" ? (
152
+ <>
153
+ <header className="anim-login__header">
154
+ <p className="anim-login__kicker">{shopName}</p>
155
+ <h1 className="anim-login__title">{title}</h1>
156
+ {subtitle ? <p className="anim-login__subtitle">{subtitle}</p> : null}
157
+ </header>
158
+
159
+ <div className="anim-login__google">
160
+ <GoogleAuthSection countryCode={countryCode} />
161
+ </div>
162
+
163
+ <div className="anim-login__divider">
164
+ <span>Or continue with email / mobile</span>
165
+ </div>
166
+
167
+ <div className="anim-login__tabs" role="tablist">
168
+ <button
169
+ type="button"
170
+ role="tab"
171
+ aria-selected={authMethod === "phone_auth"}
172
+ className={`anim-login__tab${authMethod === "phone_auth" ? " is-active" : ""}`}
173
+ onClick={() => {
174
+ if (authMethod === "phone_auth") return
175
+ setAuthMethod("phone_auth")
176
+ setError(null)
177
+ }}
178
+ >
179
+ Mobile
180
+ </button>
181
+ <button
182
+ type="button"
183
+ role="tab"
184
+ aria-selected={authMethod === "email_auth"}
185
+ className={`anim-login__tab${authMethod === "email_auth" ? " is-active" : ""}`}
186
+ onClick={() => {
187
+ if (authMethod === "email_auth") return
188
+ setAuthMethod("email_auth")
189
+ setError(null)
190
+ }}
191
+ >
192
+ Email
193
+ </button>
194
+ </div>
195
+
196
+ <div className="anim-login__identifier">
197
+ <div
198
+ className={`anim-login__phone${authMethod === "phone_auth" ? "" : " is-inactive"}`}
199
+ inert={authMethod !== "phone_auth"}
200
+ >
201
+ <SplitPhoneInput
202
+ value={phone}
203
+ onChange={setPhone}
204
+ label="Mobile number"
205
+ numberInputId="account-login-phone"
206
+ data-testid="account-login-phone"
207
+ />
208
+ </div>
209
+ <label
210
+ className={`anim-login__field${authMethod === "email_auth" ? "" : " is-inactive"}`}
211
+ inert={authMethod !== "email_auth"}
212
+ >
213
+ <span className="anim-login__label">Email</span>
214
+ <input
215
+ type="email"
216
+ name="identifier"
217
+ value={email}
218
+ onChange={(e) => setEmail(e.target.value)}
219
+ autoComplete="email"
220
+ placeholder="Enter your email"
221
+ className="anim-login__input"
222
+ tabIndex={authMethod === "email_auth" ? 0 : -1}
223
+ />
224
+ </label>
225
+ </div>
226
+
227
+ {error ? (
228
+ <p className="anim-login__error" role="alert">
229
+ {error}
230
+ </p>
231
+ ) : null}
232
+
233
+ <button
234
+ type="button"
235
+ disabled={sending || !identifierReady}
236
+ onClick={() => void handleSendOtp()}
237
+ className="anim-login__submit"
238
+ >
239
+ {sending ? "Continue…" : "Continue"}
240
+ </button>
241
+
242
+ <p className="anim-login__legal">
243
+ By continuing, you agree to {shopName}&apos;s{" "}
244
+ <LocalizedLink href={privacyHref} className="anim-login__link">
245
+ Privacy Policy
246
+ </LocalizedLink>{" "}
247
+ and{" "}
248
+ <LocalizedLink href={termsHref} className="anim-login__link">
249
+ Terms of Use
250
+ </LocalizedLink>
251
+ .
252
+ </p>
253
+
254
+ {onTrackGuest ? (
255
+ <button
256
+ type="button"
257
+ onClick={onTrackGuest}
258
+ className="anim-login__guest"
259
+ data-testid="track-guest-order-button"
260
+ >
261
+ Track guest order
262
+ </button>
263
+ ) : null}
264
+ </>
265
+ ) : (
266
+ <div className="anim-login__verify">
267
+ <header className="anim-login__header">
268
+ <p className="anim-login__kicker">Verify</p>
269
+ <h2 className="anim-login__title">Enter your code</h2>
270
+ <p className="anim-login__otp-hint" aria-live="polite">
271
+ {sending ? (
272
+ <>Sending code to {identifierDisplay}…</>
273
+ ) : (
274
+ <>
275
+ We sent a 6-digit code to{" "}
276
+ <span className="anim-login__otp-target">{identifierDisplay}</span>
277
+ </>
278
+ )}
279
+ </p>
280
+ </header>
281
+
282
+ {isNewUser ? (
283
+ <label className="anim-login__field">
284
+ <span className="anim-login__label">First name</span>
285
+ <input
286
+ type="text"
287
+ name="first_name"
288
+ value={firstName}
289
+ onChange={(e) => setFirstName(e.target.value)}
290
+ placeholder="Your first name"
291
+ required
292
+ className="anim-login__input"
293
+ />
294
+ </label>
295
+ ) : null}
296
+
297
+ <div className="anim-login__otp">
298
+ <OtpInput
299
+ value={otp}
300
+ onChange={setOtp}
301
+ autoFocus={!isNewUser && !sending && Boolean(otpToken)}
302
+ />
303
+ </div>
304
+
305
+ {error ? (
306
+ <p className="anim-login__error" role="alert">
307
+ {error}
308
+ </p>
309
+ ) : null}
310
+
311
+ <button
312
+ type="button"
313
+ disabled={
314
+ sending ||
315
+ verifying ||
316
+ !otpToken ||
317
+ otp.length < 6 ||
318
+ (isNewUser && !firstName.trim())
319
+ }
320
+ onClick={() => void handleVerify()}
321
+ className="anim-login__submit"
322
+ >
323
+ {sending
324
+ ? "Waiting for code…"
325
+ : verifying
326
+ ? "Verifying…"
327
+ : isNewUser
328
+ ? "Verify & create account"
329
+ : "Verify & sign in"}
330
+ </button>
331
+
332
+ <div className="anim-login__verify-actions">
333
+ <button
334
+ type="button"
335
+ onClick={resetToIdentifier}
336
+ disabled={verifying}
337
+ className="anim-login__text-btn"
338
+ >
339
+ Change {channelLabel}
340
+ </button>
341
+ <button
342
+ type="button"
343
+ disabled={sending || verifying || resendIn > 0}
344
+ onClick={() => void handleSendOtp({ resend: true })}
345
+ className="anim-login__text-btn"
346
+ >
347
+ {sending
348
+ ? "Resending…"
349
+ : resendIn > 0
350
+ ? `Resend in ${resendIn}s`
351
+ : "Resend OTP"}
352
+ </button>
353
+ </div>
354
+ </div>
355
+ )}
356
+ </div>
357
+ )
358
+ }
@@ -0,0 +1,11 @@
1
+ import type { SegmentManifest } from "@pradip1995/plugin-sdk"
2
+
3
+ const manifest: SegmentManifest = {
4
+ id: "animation-login",
5
+ type: "segment",
6
+ version: "0.1.0",
7
+ compatibleFramework: ["^1.0.0"],
8
+ dataKey: "account",
9
+ }
10
+
11
+ export default manifest
@@ -0,0 +1,147 @@
1
+ "use client"
2
+
3
+ import { useEffect, useState } from "react"
4
+ import { useSearchParams } from "next/navigation"
5
+ import type { HttpTypes } from "@medusajs/types"
6
+ import {
7
+ resolveOtpComponent,
8
+ type OtpVerificationComponent,
9
+ } from "@pradip1995/segment-login-template/otp-component"
10
+ import { listGuestOrders } from "@pradip1995/commerce-core/data/guest"
11
+ import AnimationLoginForm from "./login-form"
12
+ import AnimationGuestOrderModal from "./guest-order-modal"
13
+ import AnimationAccountPages from "./account-pages"
14
+ import "./animation-login.css"
15
+
16
+ type BrandFeatureCard = {
17
+ imageSrc?: string | null
18
+ title: string
19
+ text: string
20
+ }
21
+
22
+ type BrandPanelConfig = {
23
+ image?: string | null
24
+ logo?: string | null
25
+ eyebrow?: string | null
26
+ headline?: string | null
27
+ title?: string | null
28
+ description?: string | null
29
+ featuresLabel?: string | null
30
+ featureCards?: BrandFeatureCard[] | null
31
+ }
32
+
33
+ type AnimationLoginProps = {
34
+ countryCode?: string
35
+ path?: string
36
+ customer?: HttpTypes.StoreCustomer | null
37
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
38
+ paymentDetails?: Record<string, unknown>[]
39
+ guestOrders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
40
+ title?: string
41
+ subtitle?: string
42
+ shopName?: string
43
+ privacyHref?: string
44
+ termsHref?: string
45
+ brandPanel?: BrandPanelConfig | null
46
+ guestTrackLabel?: string
47
+ otpComponent?: OtpVerificationComponent | string
48
+ authMode?: "otp" | "full"
49
+ }
50
+
51
+ function accountSectionPath(path?: string): string {
52
+ if (!path || path === "login") return "overview"
53
+ const base = path.split("/")[0]
54
+ return base || "overview"
55
+ }
56
+
57
+ export default function AnimationLogin(props: AnimationLoginProps) {
58
+ const {
59
+ countryCode = "in",
60
+ path = "",
61
+ customer,
62
+ title = "Sign in",
63
+ subtitle = "OTP access for members and trail notes",
64
+ shopName = "Lumière",
65
+ privacyHref = "/privacy-policy",
66
+ termsHref = "/terms-of-use",
67
+ guestTrackLabel = "Track guest order",
68
+ otpComponent,
69
+ } = props
70
+
71
+ const OtpComponent = resolveOtpComponent(otpComponent)
72
+ const section = accountSectionPath(path)
73
+ const searchParams = useSearchParams()
74
+ const isLoggedIn = Boolean(customer && customer.id !== "pending_deletion")
75
+
76
+ const [showGuestModal, setShowGuestModal] = useState(false)
77
+ const [checkingGuest, setCheckingGuest] = useState(false)
78
+ const [guestEmail, setGuestEmail] = useState("")
79
+ const [guestAutoStart, setGuestAutoStart] = useState(false)
80
+
81
+ useEffect(() => {
82
+ const guestView = searchParams.get("view")
83
+ const email = searchParams.get("email")
84
+ if (guestView === "guest") {
85
+ setGuestEmail(email && email !== "undefined" ? email : "")
86
+ setGuestAutoStart(Boolean(email && email !== "undefined"))
87
+ setShowGuestModal(true)
88
+ }
89
+ }, [searchParams])
90
+
91
+ async function handleTrackGuestOrder() {
92
+ setCheckingGuest(true)
93
+ try {
94
+ await listGuestOrders("")
95
+ window.location.href = `/${countryCode}/account/guest-orders`
96
+ return
97
+ } catch {
98
+ // No valid guest session — open OTP modal
99
+ } finally {
100
+ setCheckingGuest(false)
101
+ }
102
+
103
+ setGuestEmail(searchParams.get("email") || "")
104
+ setGuestAutoStart(false)
105
+ setShowGuestModal(true)
106
+ }
107
+
108
+ if (isLoggedIn || section === "guest-orders") {
109
+ return <AnimationAccountPages {...props} otpComponent={OtpComponent} />
110
+ }
111
+
112
+ return (
113
+ <>
114
+ <div className="anim-login" data-login-viewport data-testid="segment-animation-login">
115
+ <div className="anim-login__form">
116
+ <AnimationLoginForm
117
+ countryCode={countryCode}
118
+ title={title}
119
+ subtitle={subtitle}
120
+ shopName={shopName}
121
+ privacyHref={privacyHref}
122
+ termsHref={termsHref}
123
+ onTrackGuest={() => {
124
+ if (checkingGuest) return
125
+ void handleTrackGuestOrder()
126
+ }}
127
+ />
128
+ {checkingGuest ? (
129
+ <p className="anim-login__guest-status">{guestTrackLabel}…</p>
130
+ ) : null}
131
+ </div>
132
+ </div>
133
+
134
+ <AnimationGuestOrderModal
135
+ open={showGuestModal}
136
+ onClose={() => {
137
+ setShowGuestModal(false)
138
+ setGuestAutoStart(false)
139
+ }}
140
+ countryCode={countryCode}
141
+ initialEmail={guestEmail}
142
+ autoStart={guestAutoStart}
143
+ OtpComponent={OtpComponent}
144
+ />
145
+ </>
146
+ )
147
+ }