@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,215 +1,215 @@
1
- "use client"
2
-
3
- import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
- import { useEffect, useRef, useState } from "react"
5
- import { sendOTP, verifyOTP } from "./auth-server"
6
- import OtpVerificationModal from "./otp-verification-modal"
7
- import type { OtpVerificationComponent } from "./otp-component"
8
-
9
- type GuestOrderModalProps = {
10
- open: boolean
11
- onClose: () => void
12
- countryCode: string
13
- initialEmail?: string
14
- autoStart?: boolean
15
- OtpComponent?: OtpVerificationComponent
16
- }
17
-
18
- export default function GuestOrderModal({
19
- open,
20
- onClose,
21
- countryCode,
22
- initialEmail = "",
23
- autoStart = false,
24
- OtpComponent = OtpVerificationModal,
25
- }: GuestOrderModalProps) {
26
- const [email, setEmail] = useState(initialEmail)
27
- const [otp, setOtp] = useState("")
28
- const [otpToken, setOtpToken] = useState<string | null>(null)
29
- const [step, setStep] = useState<"email" | "otp">("email")
30
- const [pending, setPending] = useState(false)
31
- const [error, setError] = useState<string | null>(null)
32
- const [success, setSuccess] = useState(false)
33
- const autoStartedRef = useRef(false)
34
-
35
- useEffect(() => {
36
- if (initialEmail) setEmail(initialEmail)
37
- }, [initialEmail])
38
-
39
- useEffect(() => {
40
- if (open && autoStart && initialEmail && !autoStartedRef.current) {
41
- autoStartedRef.current = true
42
- void handleSendOtp(initialEmail)
43
- }
44
- if (!open) {
45
- autoStartedRef.current = false
46
- }
47
- // eslint-disable-next-line react-hooks/exhaustive-deps
48
- }, [open, autoStart, initialEmail])
49
-
50
- async function handleSendOtp(emailToSend = email) {
51
- if (!emailToSend.trim()) return
52
- setPending(true)
53
- setError(null)
54
-
55
- try {
56
- const res = await sendOTP(emailToSend.trim())
57
- if (!res.success && (res as { error?: string }).error) {
58
- throw new Error((res as { error?: string }).error)
59
- }
60
- setOtpToken((res as { token?: string }).token || "guest")
61
- setOtp("")
62
- setStep("otp")
63
- } catch (err) {
64
- if (isNextRedirect(err)) throw err
65
- setError(safeErrorMessage(err, "Failed to send code"))
66
- setStep("email")
67
- } finally {
68
- setPending(false)
69
- }
70
- }
71
-
72
- async function handleVerifyOtp() {
73
- if (!otp || otp.length < 6) return
74
- setPending(true)
75
- setError(null)
76
-
77
- try {
78
- const res = await verifyOTP(email.trim(), otpToken || "", otp)
79
- if (!res.success) {
80
- throw new Error((res as { error?: string }).error || "Invalid code")
81
- }
82
- // verifyOTP sets httpOnly `_medusa_guest_jwt` via the server action
83
- setSuccess(true)
84
- window.setTimeout(() => {
85
- window.location.href = `/${countryCode}/account/guest-orders`
86
- }, 900)
87
- } catch (err) {
88
- if (isNextRedirect(err)) throw err
89
- setError(safeErrorMessage(err, "Verification failed"))
90
- setPending(false)
91
- }
92
- }
93
-
94
- async function handleResend() {
95
- await handleSendOtp(email)
96
- }
97
-
98
- function handleClose() {
99
- setEmail(initialEmail || "")
100
- setOtp("")
101
- setOtpToken(null)
102
- setStep("email")
103
- setError(null)
104
- setSuccess(false)
105
- setPending(false)
106
- autoStartedRef.current = false
107
- onClose()
108
- }
109
-
110
- if (!open) return null
111
-
112
- if (step === "otp" || success) {
113
- return (
114
- <OtpComponent
115
- open
116
- preventClose={!success}
117
- title={success ? "Verified" : "Verify your email"}
118
- description={
119
- success
120
- ? "Opening your guest orders…"
121
- : `Enter the 6-digit code sent to ${email || "your email"}.`
122
- }
123
- otp={otp}
124
- onOtpChange={setOtp}
125
- error={error}
126
- pending={pending}
127
- success={success}
128
- successTitle="Verification successful"
129
- successMessage="Redirecting to your guest orders…"
130
- verifyLabel="Verify & view orders"
131
- onVerify={handleVerifyOtp}
132
- onResend={handleResend}
133
- onClose={handleClose}
134
- extraContent={
135
- !success ? (
136
- <button
137
- type="button"
138
- onClick={() => {
139
- setStep("email")
140
- setOtp("")
141
- setError(null)
142
- }}
143
- className="text-xs text-muted hover:text-brand-accent mb-4 w-full text-center"
144
- >
145
- Change email
146
- </button>
147
- ) : undefined
148
- }
149
- />
150
- )
151
- }
152
-
153
- return (
154
- <div className="sr-otp-modal fixed inset-0 z-50 flex items-center justify-center p-4">
155
- <button
156
- type="button"
157
- className="sr-otp-modal__backdrop absolute inset-0"
158
- aria-label="Close guest order dialog"
159
- onClick={handleClose}
160
- />
161
- <div className="sr-otp-modal__panel relative w-full max-w-md p-7 sm:p-8">
162
- <form
163
- onSubmit={(e) => {
164
- e.preventDefault()
165
- void handleSendOtp()
166
- }}
167
- className="space-y-5"
168
- >
169
- <div className="text-center">
170
- <h2 className="sr-otp-modal__title text-xl xl:text-2xl">Track your order</h2>
171
- <p className="sr-otp-modal__copy mt-2 text-sm leading-relaxed">
172
- Enter the email used at checkout. We&apos;ll send a one-time code to view guest
173
- orders.
174
- </p>
175
- </div>
176
-
177
- <label className="block">
178
- <span className="sr-otp-modal__label mb-1.5 block">Email</span>
179
- <input
180
- type="email"
181
- value={email}
182
- onChange={(e) => setEmail(e.target.value)}
183
- required
184
- autoComplete="email"
185
- placeholder="you@example.com"
186
- className="sr-otp-modal__input"
187
- />
188
- </label>
189
-
190
- {error && (
191
- <p className="sr-otp-modal__error text-sm" role="alert">
192
- {error}
193
- </p>
194
- )}
195
-
196
- <button
197
- type="submit"
198
- disabled={pending || !email.trim()}
199
- className="sr-otp-modal__verify w-full"
200
- >
201
- {pending ? "Sending…" : "Send verification code"}
202
- </button>
203
-
204
- <button
205
- type="button"
206
- onClick={handleClose}
207
- className="sr-otp-modal__resend w-full text-center"
208
- >
209
- Cancel
210
- </button>
211
- </form>
212
- </div>
213
- </div>
214
- )
215
- }
1
+ "use client"
2
+
3
+ import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
+ import { useEffect, useRef, useState } from "react"
5
+ import { sendOTP, verifyOTP } from "./auth-server"
6
+ import OtpVerificationModal from "./otp-verification-modal"
7
+ import type { OtpVerificationComponent } from "./otp-component"
8
+
9
+ type GuestOrderModalProps = {
10
+ open: boolean
11
+ onClose: () => void
12
+ countryCode: string
13
+ initialEmail?: string
14
+ autoStart?: boolean
15
+ OtpComponent?: OtpVerificationComponent
16
+ }
17
+
18
+ export default function GuestOrderModal({
19
+ open,
20
+ onClose,
21
+ countryCode,
22
+ initialEmail = "",
23
+ autoStart = false,
24
+ OtpComponent = OtpVerificationModal,
25
+ }: GuestOrderModalProps) {
26
+ const [email, setEmail] = useState(initialEmail)
27
+ const [otp, setOtp] = useState("")
28
+ const [otpToken, setOtpToken] = useState<string | null>(null)
29
+ const [step, setStep] = useState<"email" | "otp">("email")
30
+ const [pending, setPending] = useState(false)
31
+ const [error, setError] = useState<string | null>(null)
32
+ const [success, setSuccess] = useState(false)
33
+ const autoStartedRef = useRef(false)
34
+
35
+ useEffect(() => {
36
+ if (initialEmail) setEmail(initialEmail)
37
+ }, [initialEmail])
38
+
39
+ useEffect(() => {
40
+ if (open && autoStart && initialEmail && !autoStartedRef.current) {
41
+ autoStartedRef.current = true
42
+ void handleSendOtp(initialEmail)
43
+ }
44
+ if (!open) {
45
+ autoStartedRef.current = false
46
+ }
47
+ // eslint-disable-next-line react-hooks/exhaustive-deps
48
+ }, [open, autoStart, initialEmail])
49
+
50
+ async function handleSendOtp(emailToSend = email) {
51
+ if (!emailToSend.trim()) return
52
+ setPending(true)
53
+ setError(null)
54
+
55
+ try {
56
+ const res = await sendOTP(emailToSend.trim())
57
+ if (!res.success && (res as { error?: string }).error) {
58
+ throw new Error((res as { error?: string }).error)
59
+ }
60
+ setOtpToken((res as { token?: string }).token || "guest")
61
+ setOtp("")
62
+ setStep("otp")
63
+ } catch (err) {
64
+ if (isNextRedirect(err)) throw err
65
+ setError(safeErrorMessage(err, "Failed to send code"))
66
+ setStep("email")
67
+ } finally {
68
+ setPending(false)
69
+ }
70
+ }
71
+
72
+ async function handleVerifyOtp() {
73
+ if (!otp || otp.length < 6) return
74
+ setPending(true)
75
+ setError(null)
76
+
77
+ try {
78
+ const res = await verifyOTP(email.trim(), otpToken || "", otp)
79
+ if (!res.success) {
80
+ throw new Error((res as { error?: string }).error || "Invalid code")
81
+ }
82
+ // verifyOTP sets httpOnly `_medusa_guest_jwt` via the server action
83
+ setSuccess(true)
84
+ window.setTimeout(() => {
85
+ window.location.href = `/${countryCode}/account/guest-orders`
86
+ }, 900)
87
+ } catch (err) {
88
+ if (isNextRedirect(err)) throw err
89
+ setError(safeErrorMessage(err, "Verification failed"))
90
+ setPending(false)
91
+ }
92
+ }
93
+
94
+ async function handleResend() {
95
+ await handleSendOtp(email)
96
+ }
97
+
98
+ function handleClose() {
99
+ setEmail(initialEmail || "")
100
+ setOtp("")
101
+ setOtpToken(null)
102
+ setStep("email")
103
+ setError(null)
104
+ setSuccess(false)
105
+ setPending(false)
106
+ autoStartedRef.current = false
107
+ onClose()
108
+ }
109
+
110
+ if (!open) return null
111
+
112
+ if (step === "otp" || success) {
113
+ return (
114
+ <OtpComponent
115
+ open
116
+ preventClose={!success}
117
+ title={success ? "Verified" : "Verify your email"}
118
+ description={
119
+ success
120
+ ? "Opening your guest orders…"
121
+ : `Enter the 6-digit code sent to ${email || "your email"}.`
122
+ }
123
+ otp={otp}
124
+ onOtpChange={setOtp}
125
+ error={error}
126
+ pending={pending}
127
+ success={success}
128
+ successTitle="Verification successful"
129
+ successMessage="Redirecting to your guest orders…"
130
+ verifyLabel="Verify & view orders"
131
+ onVerify={handleVerifyOtp}
132
+ onResend={handleResend}
133
+ onClose={handleClose}
134
+ extraContent={
135
+ !success ? (
136
+ <button
137
+ type="button"
138
+ onClick={() => {
139
+ setStep("email")
140
+ setOtp("")
141
+ setError(null)
142
+ }}
143
+ className="text-xs text-muted hover:text-brand-accent mb-4 w-full text-center"
144
+ >
145
+ Change email
146
+ </button>
147
+ ) : undefined
148
+ }
149
+ />
150
+ )
151
+ }
152
+
153
+ return (
154
+ <div className="sr-otp-modal fixed inset-0 z-50 flex items-center justify-center p-4">
155
+ <button
156
+ type="button"
157
+ className="sr-otp-modal__backdrop absolute inset-0"
158
+ aria-label="Close guest order dialog"
159
+ onClick={handleClose}
160
+ />
161
+ <div className="sr-otp-modal__panel relative w-full max-w-md p-7 sm:p-8">
162
+ <form
163
+ onSubmit={(e) => {
164
+ e.preventDefault()
165
+ void handleSendOtp()
166
+ }}
167
+ className="space-y-5"
168
+ >
169
+ <div className="text-center">
170
+ <h2 className="sr-otp-modal__title text-xl xl:text-2xl">Track your order</h2>
171
+ <p className="sr-otp-modal__copy mt-2 text-sm leading-relaxed">
172
+ Enter the email used at checkout. We&apos;ll send a one-time code to view guest
173
+ orders.
174
+ </p>
175
+ </div>
176
+
177
+ <label className="block">
178
+ <span className="sr-otp-modal__label mb-1.5 block">Email</span>
179
+ <input
180
+ type="email"
181
+ value={email}
182
+ onChange={(e) => setEmail(e.target.value)}
183
+ required
184
+ autoComplete="email"
185
+ placeholder="you@example.com"
186
+ className="sr-otp-modal__input"
187
+ />
188
+ </label>
189
+
190
+ {error && (
191
+ <p className="sr-otp-modal__error text-sm" role="alert">
192
+ {error}
193
+ </p>
194
+ )}
195
+
196
+ <button
197
+ type="submit"
198
+ disabled={pending || !email.trim()}
199
+ className="sr-otp-modal__verify w-full"
200
+ >
201
+ {pending ? "Sending…" : "Send verification code"}
202
+ </button>
203
+
204
+ <button
205
+ type="button"
206
+ onClick={handleClose}
207
+ className="sr-otp-modal__resend w-full text-center"
208
+ >
209
+ Cancel
210
+ </button>
211
+ </form>
212
+ </div>
213
+ </div>
214
+ )
215
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { default } from "./segment"
2
- export { default as manifest } from "./manifest"
3
- export { default as ResetPasswordPage } from "./reset-password-page"
4
- export { default as OtpVerificationModal } from "./otp-verification-modal"
5
- export { default as CommerceAuthOtpModal } from "./commerce-auth-otp-modal"
6
- export type { OtpVerificationModalProps } from "./otp-verification-modal"
7
- export { resolveOtpComponent, type OtpVerificationComponent } from "./otp-component"
1
+ export { default } from "./segment"
2
+ export { default as manifest } from "./manifest"
3
+ export { default as ResetPasswordPage } from "./reset-password-page"
4
+ export { default as OtpVerificationModal } from "./otp-verification-modal"
5
+ export { default as CommerceAuthOtpModal } from "./commerce-auth-otp-modal"
6
+ export type { OtpVerificationModalProps } from "./otp-verification-modal"
7
+ export { resolveOtpComponent, type OtpVerificationComponent } from "./otp-component"
@@ -1,60 +1,60 @@
1
- /**
2
- * Shared Next.js redirect/notFound detection for segment-login-template.
3
- * Keep in sync with @pradip1995/segment-primitives/is-next-redirect.
4
- */
5
-
6
- const CONTROL_FLOW_RE = /NEXT_REDIRECT|NEXT_REDIRECTION|NEXT_NOT_FOUND|NEXT_HTTP_ERROR_FALLBACK/i
7
-
8
- function textLooksLikeControlFlow(value: string): boolean {
9
- return CONTROL_FLOW_RE.test(value)
10
- }
11
-
12
- export function isNextRedirect(err: unknown): boolean {
13
- if (err == null) return false
14
-
15
- if (typeof err === "string") {
16
- return textLooksLikeControlFlow(err)
17
- }
18
-
19
- if (typeof err !== "object") {
20
- return textLooksLikeControlFlow(String(err))
21
- }
22
-
23
- const digest =
24
- "digest" in err ? String((err as { digest?: unknown }).digest ?? "") : ""
25
- if (textLooksLikeControlFlow(digest)) {
26
- return true
27
- }
28
-
29
- const message =
30
- err instanceof Error
31
- ? err.message
32
- : "message" in err
33
- ? String((err as { message?: unknown }).message ?? "")
34
- : ""
35
-
36
- if (textLooksLikeControlFlow(message)) {
37
- return true
38
- }
39
-
40
- try {
41
- return textLooksLikeControlFlow(String(err))
42
- } catch {
43
- return false
44
- }
45
- }
46
-
47
- export function safeErrorMessage(err: unknown, fallback: string): string {
48
- if (isNextRedirect(err)) return fallback
49
- if (err instanceof Error) {
50
- const msg = err.message?.trim()
51
- if (!msg || textLooksLikeControlFlow(msg)) return fallback
52
- return msg
53
- }
54
- if (typeof err === "string") {
55
- const msg = err.trim()
56
- if (!msg || textLooksLikeControlFlow(msg)) return fallback
57
- return msg
58
- }
59
- return fallback
60
- }
1
+ /**
2
+ * Shared Next.js redirect/notFound detection for segment-login-template.
3
+ * Keep in sync with @pradip1995/segment-primitives/is-next-redirect.
4
+ */
5
+
6
+ const CONTROL_FLOW_RE = /NEXT_REDIRECT|NEXT_REDIRECTION|NEXT_NOT_FOUND|NEXT_HTTP_ERROR_FALLBACK/i
7
+
8
+ function textLooksLikeControlFlow(value: string): boolean {
9
+ return CONTROL_FLOW_RE.test(value)
10
+ }
11
+
12
+ export function isNextRedirect(err: unknown): boolean {
13
+ if (err == null) return false
14
+
15
+ if (typeof err === "string") {
16
+ return textLooksLikeControlFlow(err)
17
+ }
18
+
19
+ if (typeof err !== "object") {
20
+ return textLooksLikeControlFlow(String(err))
21
+ }
22
+
23
+ const digest =
24
+ "digest" in err ? String((err as { digest?: unknown }).digest ?? "") : ""
25
+ if (textLooksLikeControlFlow(digest)) {
26
+ return true
27
+ }
28
+
29
+ const message =
30
+ err instanceof Error
31
+ ? err.message
32
+ : "message" in err
33
+ ? String((err as { message?: unknown }).message ?? "")
34
+ : ""
35
+
36
+ if (textLooksLikeControlFlow(message)) {
37
+ return true
38
+ }
39
+
40
+ try {
41
+ return textLooksLikeControlFlow(String(err))
42
+ } catch {
43
+ return false
44
+ }
45
+ }
46
+
47
+ export function safeErrorMessage(err: unknown, fallback: string): string {
48
+ if (isNextRedirect(err)) return fallback
49
+ if (err instanceof Error) {
50
+ const msg = err.message?.trim()
51
+ if (!msg || textLooksLikeControlFlow(msg)) return fallback
52
+ return msg
53
+ }
54
+ if (typeof err === "string") {
55
+ const msg = err.trim()
56
+ if (!msg || textLooksLikeControlFlow(msg)) return fallback
57
+ return msg
58
+ }
59
+ return fallback
60
+ }