@pradip1995/segment-login-template 0.5.25 → 0.5.27

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 +2 -2
  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 +6 -5
  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
package/src/segment.tsx CHANGED
@@ -1,272 +1,272 @@
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 LoginForm from "./login-form"
7
- import RegisterForm from "./register-form"
8
- import ForgotPasswordForm from "./forgot-password-form"
9
- import AccountOverview from "./account-overview"
10
- import AccountProfile from "./account-profile"
11
- import AccountAddresses from "./account-addresses"
12
- import AccountOrders from "./account-orders"
13
- import AccountPaymentMethods from "./account-payment-methods"
14
- import AccountGuestOrders from "./account-guest-orders"
15
- import LoginBrandPanel from "./login-brand-panel"
16
- import GuestOrderModal from "./guest-order-modal"
17
- import OtpUnifiedForm from "./otp-unified-form"
18
- import { resolveOtpComponent, type OtpVerificationComponent } from "./otp-component"
19
- import { accountSectionPath } from "./account-utils"
20
- import { auth, type BrandPanelConfig } from "./account-theme"
21
- import { listGuestOrders } from "@pradip1995/commerce-core/data/guest"
22
-
23
- type AccountProps = {
24
- countryCode?: string
25
- path?: string
26
- customer?: HttpTypes.StoreCustomer | null
27
- orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
28
- paymentDetails?: Record<string, unknown>[]
29
- guestOrders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
30
- title?: string
31
- subtitle?: string
32
- registerTitle?: string
33
- registerSubtitle?: string
34
- brandPanel?: BrandPanelConfig | null
35
- guestTrackLabel?: string
36
- otpComponent?: OtpVerificationComponent
37
- /**
38
- * `otp` — Sahsha single-screen (Google + Mobile/Email OTP, signup in verify step).
39
- * `full` — password + OTP tabs with separate register form.
40
- */
41
- authMode?: "otp" | "full"
42
- }
43
-
44
- export default function LoginTemplate({
45
- countryCode = "in",
46
- path = "",
47
- customer,
48
- orders = [],
49
- paymentDetails = [],
50
- guestOrders = [],
51
- title = "Sign in",
52
- subtitle = "Access your exclusive collection",
53
- registerTitle = "Create account",
54
- registerSubtitle = "Join our exclusive collection",
55
- brandPanel,
56
- guestTrackLabel = "Track guest order",
57
- otpComponent,
58
- authMode = "otp",
59
- }: AccountProps) {
60
- const OtpComponent = resolveOtpComponent(otpComponent)
61
- const section = accountSectionPath(path)
62
- const searchParams = useSearchParams()
63
-
64
- const [view, setView] = useState<"login" | "register" | "forgot">(
65
- path === "register" ? "register" : path === "forgot-password" ? "forgot" : "login"
66
- )
67
- const [showGuestModal, setShowGuestModal] = useState(false)
68
- const [checkingGuest, setCheckingGuest] = useState(false)
69
- const [guestEmail, setGuestEmail] = useState("")
70
- const [guestAutoStart, setGuestAutoStart] = useState(false)
71
-
72
- useEffect(() => {
73
- const guestView = searchParams.get("view")
74
- const email = searchParams.get("email")
75
- if (guestView === "guest") {
76
- setGuestEmail(email && email !== "undefined" ? email : "")
77
- setGuestAutoStart(Boolean(email && email !== "undefined"))
78
- setShowGuestModal(true)
79
- }
80
- }, [searchParams])
81
-
82
- async function handleTrackGuestOrder() {
83
- // Guest JWT is httpOnly (_medusa_guest_jwt); probe via server action (empty token → cookie).
84
- setCheckingGuest(true)
85
- try {
86
- await listGuestOrders("")
87
- window.location.href = `/${countryCode}/account/guest-orders`
88
- return
89
- } catch {
90
- // No valid guest session — open OTP modal
91
- } finally {
92
- setCheckingGuest(false)
93
- }
94
-
95
- setGuestEmail(searchParams.get("email") || "")
96
- setGuestAutoStart(false)
97
- setShowGuestModal(true)
98
- }
99
-
100
- if (section === "guest-orders") {
101
- return <AccountGuestOrders orders={guestOrders} countryCode={countryCode} />
102
- }
103
-
104
- if (customer && customer.id !== "pending_deletion") {
105
- switch (section) {
106
- case "profile":
107
- return (
108
- <AccountProfile
109
- customer={customer}
110
- countryCode={countryCode}
111
- OtpComponent={OtpComponent}
112
- />
113
- )
114
- case "addresses":
115
- return <AccountAddresses customer={customer} countryCode={countryCode} />
116
- case "orders":
117
- return <AccountOrders orders={orders} />
118
- case "payment-methods":
119
- return (
120
- <AccountPaymentMethods
121
- paymentDetails={
122
- paymentDetails as Parameters<typeof AccountPaymentMethods>[0]["paymentDetails"]
123
- }
124
- />
125
- )
126
- case "overview":
127
- default:
128
- return (
129
- <AccountOverview customer={customer} orders={orders} countryCode={countryCode} />
130
- )
131
- }
132
- }
133
-
134
- const isOtpMode = authMode === "otp"
135
- const isSignIn = view === "login"
136
- const isRegister = view === "register"
137
- const heading = isOtpMode
138
- ? title
139
- : isSignIn
140
- ? title
141
- : isRegister
142
- ? registerTitle
143
- : "Reset password"
144
- const subheading = isOtpMode
145
- ? subtitle
146
- : isSignIn
147
- ? subtitle
148
- : isRegister
149
- ? registerSubtitle
150
- : undefined
151
-
152
- return (
153
- <>
154
- <div data-login-viewport className={auth.viewport}>
155
- <LoginBrandPanel panel={brandPanel} variant="mobile" />
156
- <LoginBrandPanel panel={brandPanel} variant="desktop" />
157
-
158
- <div className={auth.formColumn}>
159
- <div className={auth.formWash} aria-hidden />
160
- <div className={auth.formGlow} aria-hidden />
161
-
162
- <div className={auth.formInner}>
163
- {(isOtpMode || isSignIn || isRegister) && view !== "forgot" && (
164
- <header className={auth.desktopHeader}>
165
- <div className="flex items-center gap-3 mb-3">
166
- <span className="h-px flex-1 max-w-[48px] bg-gradient-to-r from-brand-accent to-brand-primary" />
167
- <span className="text-brand-accent text-[8px] leading-none">◆</span>
168
- <span className="h-px flex-1 max-w-[48px] bg-gradient-to-l from-brand-accent to-brand-primary" />
169
- </div>
170
- <h1 className={auth.title}>{heading}</h1>
171
- {subheading && <p className={auth.subtitle}>{subheading}</p>}
172
- </header>
173
- )}
174
-
175
- <div className={auth.formCard}>
176
- {isOtpMode ? (
177
- view === "forgot" ? (
178
- <ForgotPasswordForm onBack={() => setView("login")} />
179
- ) : (
180
- <OtpUnifiedForm
181
- countryCode={countryCode}
182
- onTrackGuest={() => {
183
- if (checkingGuest) return
184
- void handleTrackGuestOrder()
185
- }}
186
- />
187
- )
188
- ) : (
189
- <>
190
- {view !== "forgot" && (
191
- <div className="flex gap-6 mb-8 border-b border-cart-border">
192
- <TabButton active={view === "login"} onClick={() => setView("login")}>
193
- Sign in
194
- </TabButton>
195
- <TabButton
196
- active={view === "register"}
197
- onClick={() => setView("register")}
198
- >
199
- Register
200
- </TabButton>
201
- </div>
202
- )}
203
-
204
- {view === "login" && (
205
- <LoginForm
206
- countryCode={countryCode}
207
- OtpComponent={OtpComponent}
208
- onForgot={() => setView("forgot")}
209
- onRegister={() => setView("register")}
210
- onTrackGuest={() => {
211
- if (checkingGuest) return
212
- void handleTrackGuestOrder()
213
- }}
214
- />
215
- )}
216
- {view === "register" && (
217
- <RegisterForm
218
- countryCode={countryCode}
219
- OtpComponent={OtpComponent}
220
- onLogin={() => setView("login")}
221
- />
222
- )}
223
- {view === "forgot" && (
224
- <ForgotPasswordForm onBack={() => setView("login")} />
225
- )}
226
- </>
227
- )}
228
- </div>
229
-
230
- {checkingGuest && (
231
- <p className="mt-4 text-xs text-muted uppercase tracking-[0.2em]">
232
- {guestTrackLabel}…
233
- </p>
234
- )}
235
- </div>
236
- </div>
237
- </div>
238
-
239
- <GuestOrderModal
240
- open={showGuestModal}
241
- onClose={() => {
242
- setShowGuestModal(false)
243
- setGuestAutoStart(false)
244
- }}
245
- countryCode={countryCode}
246
- initialEmail={guestEmail}
247
- autoStart={guestAutoStart}
248
- OtpComponent={OtpComponent}
249
- />
250
- </>
251
- )
252
- }
253
-
254
- function TabButton({
255
- active,
256
- onClick,
257
- children,
258
- }: {
259
- active: boolean
260
- onClick: () => void
261
- children: React.ReactNode
262
- }) {
263
- return (
264
- <button
265
- type="button"
266
- onClick={onClick}
267
- className={active ? auth.tabActive : auth.tabIdle}
268
- >
269
- {children}
270
- </button>
271
- )
272
- }
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 LoginForm from "./login-form"
7
+ import RegisterForm from "./register-form"
8
+ import ForgotPasswordForm from "./forgot-password-form"
9
+ import AccountOverview from "./account-overview"
10
+ import AccountProfile from "./account-profile"
11
+ import AccountAddresses from "./account-addresses"
12
+ import AccountOrders from "./account-orders"
13
+ import AccountPaymentMethods from "./account-payment-methods"
14
+ import AccountGuestOrders from "./account-guest-orders"
15
+ import LoginBrandPanel from "./login-brand-panel"
16
+ import GuestOrderModal from "./guest-order-modal"
17
+ import OtpUnifiedForm from "./otp-unified-form"
18
+ import { resolveOtpComponent, type OtpVerificationComponent } from "./otp-component"
19
+ import { accountSectionPath } from "./account-utils"
20
+ import { auth, type BrandPanelConfig } from "./account-theme"
21
+ import { listGuestOrders } from "@pradip1995/commerce-core/data/guest"
22
+
23
+ type AccountProps = {
24
+ countryCode?: string
25
+ path?: string
26
+ customer?: HttpTypes.StoreCustomer | null
27
+ orders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
28
+ paymentDetails?: Record<string, unknown>[]
29
+ guestOrders?: HttpTypes.StoreOrder[] | { orders?: HttpTypes.StoreOrder[] } | null
30
+ title?: string
31
+ subtitle?: string
32
+ registerTitle?: string
33
+ registerSubtitle?: string
34
+ brandPanel?: BrandPanelConfig | null
35
+ guestTrackLabel?: string
36
+ otpComponent?: OtpVerificationComponent
37
+ /**
38
+ * `otp` — Sahsha single-screen (Google + Mobile/Email OTP, signup in verify step).
39
+ * `full` — password + OTP tabs with separate register form.
40
+ */
41
+ authMode?: "otp" | "full"
42
+ }
43
+
44
+ export default function LoginTemplate({
45
+ countryCode = "in",
46
+ path = "",
47
+ customer,
48
+ orders = [],
49
+ paymentDetails = [],
50
+ guestOrders = [],
51
+ title = "Sign in",
52
+ subtitle = "Access your exclusive collection",
53
+ registerTitle = "Create account",
54
+ registerSubtitle = "Join our exclusive collection",
55
+ brandPanel,
56
+ guestTrackLabel = "Track guest order",
57
+ otpComponent,
58
+ authMode = "otp",
59
+ }: AccountProps) {
60
+ const OtpComponent = resolveOtpComponent(otpComponent)
61
+ const section = accountSectionPath(path)
62
+ const searchParams = useSearchParams()
63
+
64
+ const [view, setView] = useState<"login" | "register" | "forgot">(
65
+ path === "register" ? "register" : path === "forgot-password" ? "forgot" : "login"
66
+ )
67
+ const [showGuestModal, setShowGuestModal] = useState(false)
68
+ const [checkingGuest, setCheckingGuest] = useState(false)
69
+ const [guestEmail, setGuestEmail] = useState("")
70
+ const [guestAutoStart, setGuestAutoStart] = useState(false)
71
+
72
+ useEffect(() => {
73
+ const guestView = searchParams.get("view")
74
+ const email = searchParams.get("email")
75
+ if (guestView === "guest") {
76
+ setGuestEmail(email && email !== "undefined" ? email : "")
77
+ setGuestAutoStart(Boolean(email && email !== "undefined"))
78
+ setShowGuestModal(true)
79
+ }
80
+ }, [searchParams])
81
+
82
+ async function handleTrackGuestOrder() {
83
+ // Guest JWT is httpOnly (_medusa_guest_jwt); probe via server action (empty token → cookie).
84
+ setCheckingGuest(true)
85
+ try {
86
+ await listGuestOrders("")
87
+ window.location.href = `/${countryCode}/account/guest-orders`
88
+ return
89
+ } catch {
90
+ // No valid guest session — open OTP modal
91
+ } finally {
92
+ setCheckingGuest(false)
93
+ }
94
+
95
+ setGuestEmail(searchParams.get("email") || "")
96
+ setGuestAutoStart(false)
97
+ setShowGuestModal(true)
98
+ }
99
+
100
+ if (section === "guest-orders") {
101
+ return <AccountGuestOrders orders={guestOrders} countryCode={countryCode} />
102
+ }
103
+
104
+ if (customer && customer.id !== "pending_deletion") {
105
+ switch (section) {
106
+ case "profile":
107
+ return (
108
+ <AccountProfile
109
+ customer={customer}
110
+ countryCode={countryCode}
111
+ OtpComponent={OtpComponent}
112
+ />
113
+ )
114
+ case "addresses":
115
+ return <AccountAddresses customer={customer} countryCode={countryCode} />
116
+ case "orders":
117
+ return <AccountOrders orders={orders} />
118
+ case "payment-methods":
119
+ return (
120
+ <AccountPaymentMethods
121
+ paymentDetails={
122
+ paymentDetails as Parameters<typeof AccountPaymentMethods>[0]["paymentDetails"]
123
+ }
124
+ />
125
+ )
126
+ case "overview":
127
+ default:
128
+ return (
129
+ <AccountOverview customer={customer} orders={orders} countryCode={countryCode} />
130
+ )
131
+ }
132
+ }
133
+
134
+ const isOtpMode = authMode === "otp"
135
+ const isSignIn = view === "login"
136
+ const isRegister = view === "register"
137
+ const heading = isOtpMode
138
+ ? title
139
+ : isSignIn
140
+ ? title
141
+ : isRegister
142
+ ? registerTitle
143
+ : "Reset password"
144
+ const subheading = isOtpMode
145
+ ? subtitle
146
+ : isSignIn
147
+ ? subtitle
148
+ : isRegister
149
+ ? registerSubtitle
150
+ : undefined
151
+
152
+ return (
153
+ <>
154
+ <div data-login-viewport className={auth.viewport}>
155
+ <LoginBrandPanel panel={brandPanel} variant="mobile" />
156
+ <LoginBrandPanel panel={brandPanel} variant="desktop" />
157
+
158
+ <div className={auth.formColumn}>
159
+ <div className={auth.formWash} aria-hidden />
160
+ <div className={auth.formGlow} aria-hidden />
161
+
162
+ <div className={auth.formInner}>
163
+ {(isOtpMode || isSignIn || isRegister) && view !== "forgot" && (
164
+ <header className={auth.desktopHeader}>
165
+ <div className="flex items-center gap-3 mb-3">
166
+ <span className="h-px flex-1 max-w-[48px] bg-gradient-to-r from-brand-accent to-brand-primary" />
167
+ <span className="text-brand-accent text-[8px] leading-none">◆</span>
168
+ <span className="h-px flex-1 max-w-[48px] bg-gradient-to-l from-brand-accent to-brand-primary" />
169
+ </div>
170
+ <h1 className={auth.title}>{heading}</h1>
171
+ {subheading && <p className={auth.subtitle}>{subheading}</p>}
172
+ </header>
173
+ )}
174
+
175
+ <div className={auth.formCard}>
176
+ {isOtpMode ? (
177
+ view === "forgot" ? (
178
+ <ForgotPasswordForm onBack={() => setView("login")} />
179
+ ) : (
180
+ <OtpUnifiedForm
181
+ countryCode={countryCode}
182
+ onTrackGuest={() => {
183
+ if (checkingGuest) return
184
+ void handleTrackGuestOrder()
185
+ }}
186
+ />
187
+ )
188
+ ) : (
189
+ <>
190
+ {view !== "forgot" && (
191
+ <div className="flex gap-6 mb-8 border-b border-cart-border">
192
+ <TabButton active={view === "login"} onClick={() => setView("login")}>
193
+ Sign in
194
+ </TabButton>
195
+ <TabButton
196
+ active={view === "register"}
197
+ onClick={() => setView("register")}
198
+ >
199
+ Register
200
+ </TabButton>
201
+ </div>
202
+ )}
203
+
204
+ {view === "login" && (
205
+ <LoginForm
206
+ countryCode={countryCode}
207
+ OtpComponent={OtpComponent}
208
+ onForgot={() => setView("forgot")}
209
+ onRegister={() => setView("register")}
210
+ onTrackGuest={() => {
211
+ if (checkingGuest) return
212
+ void handleTrackGuestOrder()
213
+ }}
214
+ />
215
+ )}
216
+ {view === "register" && (
217
+ <RegisterForm
218
+ countryCode={countryCode}
219
+ OtpComponent={OtpComponent}
220
+ onLogin={() => setView("login")}
221
+ />
222
+ )}
223
+ {view === "forgot" && (
224
+ <ForgotPasswordForm onBack={() => setView("login")} />
225
+ )}
226
+ </>
227
+ )}
228
+ </div>
229
+
230
+ {checkingGuest && (
231
+ <p className="mt-4 text-xs text-muted uppercase tracking-[0.2em]">
232
+ {guestTrackLabel}…
233
+ </p>
234
+ )}
235
+ </div>
236
+ </div>
237
+ </div>
238
+
239
+ <GuestOrderModal
240
+ open={showGuestModal}
241
+ onClose={() => {
242
+ setShowGuestModal(false)
243
+ setGuestAutoStart(false)
244
+ }}
245
+ countryCode={countryCode}
246
+ initialEmail={guestEmail}
247
+ autoStart={guestAutoStart}
248
+ OtpComponent={OtpComponent}
249
+ />
250
+ </>
251
+ )
252
+ }
253
+
254
+ function TabButton({
255
+ active,
256
+ onClick,
257
+ children,
258
+ }: {
259
+ active: boolean
260
+ onClick: () => void
261
+ children: React.ReactNode
262
+ }) {
263
+ return (
264
+ <button
265
+ type="button"
266
+ onClick={onClick}
267
+ className={active ? auth.tabActive : auth.tabIdle}
268
+ >
269
+ {children}
270
+ </button>
271
+ )
272
+ }