@pradip1995/segment-login-template 0.5.27 → 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.
Files changed (42) hide show
  1. package/package.json +13 -9
  2. package/src/account-addresses.tsx +307 -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/address-actions.ts +74 -0
  12. package/src/auth-server.ts +442 -442
  13. package/src/card-expiry.test.ts +71 -0
  14. package/src/card-expiry.ts +29 -0
  15. package/src/commerce-auth-otp-modal.css +125 -125
  16. package/src/commerce-auth-otp-modal.tsx +121 -121
  17. package/src/default-address.test.ts +143 -0
  18. package/src/default-address.ts +51 -0
  19. package/src/forgot-password-form.tsx +109 -109
  20. package/src/google-auth-section.tsx +22 -22
  21. package/src/google-sign-in-section.tsx +67 -67
  22. package/src/guest-order-modal.tsx +215 -215
  23. package/src/index.ts +7 -7
  24. package/src/is-next-redirect.ts +60 -60
  25. package/src/login-form.tsx +469 -469
  26. package/src/manifest.ts +11 -11
  27. package/src/order-history-status.test.ts +224 -224
  28. package/src/order-history-status.ts +244 -244
  29. package/src/otp-component.ts +14 -14
  30. package/src/otp-input.tsx +46 -46
  31. package/src/otp-unified-form.tsx +333 -333
  32. package/src/otp-verification-modal.tsx +112 -112
  33. package/src/payment-methods-actions.ts +243 -243
  34. package/src/profile-email-field.tsx +116 -116
  35. package/src/profile-email.test.ts +182 -182
  36. package/src/profile-email.ts +54 -54
  37. package/src/profile-phone-field.tsx +119 -119
  38. package/src/register-form.tsx +339 -339
  39. package/src/reorder-checkout-address.ts +103 -101
  40. package/src/reset-password-form.tsx +103 -103
  41. package/src/reset-password-page.tsx +12 -12
  42. package/src/segment.tsx +272 -272
@@ -0,0 +1,143 @@
1
+ import { describe, it } from "node:test"
2
+ import assert from "node:assert/strict"
3
+ import {
4
+ findMatchingAddressId,
5
+ isExclusiveDefaultAddress,
6
+ resolveDefaultAddressId,
7
+ } from "./default-address.ts"
8
+
9
+ describe("resolveDefaultAddressId", () => {
10
+ const cases: Array<{
11
+ name: string
12
+ addresses: Parameters<typeof resolveDefaultAddressId>[0]
13
+ expected: string | null
14
+ }> = [
15
+ { name: "empty list", addresses: [], expected: null },
16
+ {
17
+ name: "no default flags",
18
+ addresses: [
19
+ { id: "a1" },
20
+ { id: "a2", is_default_shipping: false, is_default_billing: false },
21
+ ],
22
+ expected: null,
23
+ },
24
+ {
25
+ name: "only shipping default",
26
+ addresses: [
27
+ { id: "a1", is_default_shipping: true },
28
+ { id: "a2", is_default_billing: false },
29
+ ],
30
+ expected: "a1",
31
+ },
32
+ {
33
+ name: "only billing default",
34
+ addresses: [
35
+ { id: "a1", is_default_billing: true },
36
+ { id: "a2" },
37
+ ],
38
+ expected: "a1",
39
+ },
40
+ {
41
+ name: "shipping wins when two addresses claim default",
42
+ addresses: [
43
+ { id: "ship", is_default_shipping: true, is_default_billing: false },
44
+ { id: "bill", is_default_shipping: false, is_default_billing: true },
45
+ ],
46
+ expected: "ship",
47
+ },
48
+ {
49
+ name: "same address shipping and billing",
50
+ addresses: [
51
+ { id: "only", is_default_shipping: true, is_default_billing: true },
52
+ { id: "other" },
53
+ ],
54
+ expected: "only",
55
+ },
56
+ {
57
+ name: "multiple shipping defaults picks the first",
58
+ addresses: [
59
+ { id: "first", is_default_shipping: true },
60
+ { id: "second", is_default_shipping: true },
61
+ ],
62
+ expected: "first",
63
+ },
64
+ ]
65
+
66
+ for (const row of cases) {
67
+ it(row.name, () => {
68
+ assert.equal(resolveDefaultAddressId(row.addresses), row.expected)
69
+ })
70
+ }
71
+ })
72
+
73
+ describe("isExclusiveDefaultAddress", () => {
74
+ const cases: Array<{
75
+ name: string
76
+ address: { id?: string | null }
77
+ defaultAddressId: string | null
78
+ expected: boolean
79
+ }> = [
80
+ { name: "match", address: { id: "a1" }, defaultAddressId: "a1", expected: true },
81
+ { name: "other address", address: { id: "a2" }, defaultAddressId: "a1", expected: false },
82
+ { name: "no default selected", address: { id: "a1" }, defaultAddressId: null, expected: false },
83
+ { name: "missing id", address: {}, defaultAddressId: "a1", expected: false },
84
+ ]
85
+
86
+ for (const row of cases) {
87
+ it(row.name, () => {
88
+ assert.equal(
89
+ isExclusiveDefaultAddress(row.address, row.defaultAddressId),
90
+ row.expected
91
+ )
92
+ })
93
+ }
94
+ })
95
+
96
+ describe("findMatchingAddressId", () => {
97
+ const cases: Array<{
98
+ name: string
99
+ addresses: Parameters<typeof findMatchingAddressId>[0]
100
+ match: Parameters<typeof findMatchingAddressId>[1]
101
+ expected: string | null
102
+ }> = [
103
+ {
104
+ name: "no match",
105
+ addresses: [{ id: "a1", address_1: "1 Main", postal_code: "10001" }],
106
+ match: { address_1: "2 Main", postal_code: "10001" },
107
+ expected: null,
108
+ },
109
+ {
110
+ name: "unique match",
111
+ addresses: [
112
+ { id: "a1", address_1: "1 Main", postal_code: "10001" },
113
+ { id: "a2", address_1: "2 Main", postal_code: "10002" },
114
+ ],
115
+ match: { address_1: "1 Main", postal_code: "10001" },
116
+ expected: "a1",
117
+ },
118
+ {
119
+ name: "duplicate lines prefer phone match",
120
+ addresses: [
121
+ { id: "old", address_1: "1 Main", postal_code: "10001", phone: "111" },
122
+ { id: "new", address_1: "1 Main", postal_code: "10001", phone: "222" },
123
+ ],
124
+ match: { address_1: "1 Main", postal_code: "10001", phone: "222" },
125
+ expected: "new",
126
+ },
127
+ {
128
+ name: "duplicate lines without phone take last",
129
+ addresses: [
130
+ { id: "old", address_1: "1 Main", postal_code: "10001" },
131
+ { id: "new", address_1: "1 Main", postal_code: "10001" },
132
+ ],
133
+ match: { address_1: "1 Main", postal_code: "10001" },
134
+ expected: "new",
135
+ },
136
+ ]
137
+
138
+ for (const row of cases) {
139
+ it(row.name, () => {
140
+ assert.equal(findMatchingAddressId(row.addresses, row.match), row.expected)
141
+ })
142
+ }
143
+ })
@@ -0,0 +1,51 @@
1
+ export type DefaultAddressFlags = {
2
+ id?: string | null
3
+ is_default_shipping?: boolean | null
4
+ is_default_billing?: boolean | null
5
+ address_1?: string | null
6
+ postal_code?: string | null
7
+ phone?: string | null
8
+ }
9
+
10
+ /** One default only: shipping wins, then billing. */
11
+ export function resolveDefaultAddressId(
12
+ addresses: DefaultAddressFlags[]
13
+ ): string | null {
14
+ const shipping = addresses.find((address) => address.is_default_shipping && address.id)
15
+ if (shipping?.id) return shipping.id
16
+ const billing = addresses.find((address) => address.is_default_billing && address.id)
17
+ return billing?.id ?? null
18
+ }
19
+
20
+ export function isExclusiveDefaultAddress(
21
+ address: DefaultAddressFlags,
22
+ defaultAddressId: string | null
23
+ ): boolean {
24
+ return Boolean(address.id) && address.id === defaultAddressId
25
+ }
26
+
27
+ /** Match a just-saved address so we can force exclusive default after create. */
28
+ export function findMatchingAddressId(
29
+ addresses: DefaultAddressFlags[],
30
+ match: { address_1: string; postal_code: string; phone?: string }
31
+ ): string | null {
32
+ const address1 = match.address_1.trim()
33
+ const postal = match.postal_code.trim()
34
+ const phone = match.phone?.trim() || ""
35
+
36
+ const candidates = addresses.filter((address) => {
37
+ if (!address.id) return false
38
+ if ((address.address_1 || "").trim() !== address1) return false
39
+ if ((address.postal_code || "").trim() !== postal) return false
40
+ if (phone && (address.phone || "").trim() !== phone) return false
41
+ return true
42
+ })
43
+
44
+ if (candidates.length === 0) return null
45
+ if (candidates.length === 1) return candidates[0].id!
46
+
47
+ const withPhone = phone
48
+ ? candidates.find((address) => (address.phone || "").trim() === phone)
49
+ : undefined
50
+ return (withPhone || candidates[candidates.length - 1]).id!
51
+ }
@@ -1,109 +1,109 @@
1
- "use client"
2
-
3
- import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
- import { useState } from "react"
5
- import { checkEmailRegistered, requestPasswordReset } from "./auth-server"
6
- import { auth } from "./account-theme"
7
- import { AuthField, Ornament } from "./login-form"
8
-
9
- export default function ForgotPasswordForm({ onBack }: { onBack: () => void }) {
10
- const [email, setEmail] = useState("")
11
- const [message, setMessage] = useState<string | null>(null)
12
- const [error, setError] = useState<string | null>(null)
13
- const [pending, setPending] = useState(false)
14
- const [success, setSuccess] = useState(false)
15
-
16
- async function handleSubmit(e: React.FormEvent) {
17
- e.preventDefault()
18
- if (!email.trim()) return
19
-
20
- setPending(true)
21
- setMessage(null)
22
- setError(null)
23
-
24
- try {
25
- const check = await checkEmailRegistered(email.trim())
26
- if (!check.exists) {
27
- setError("This email is not registered. Please create a new account instead.")
28
- return
29
- }
30
-
31
- const result = await requestPasswordReset(email.trim())
32
- if (!result.success) {
33
- setError(result.error || "Failed to send reset link")
34
- return
35
- }
36
-
37
- setSuccess(true)
38
- setMessage(result.message || "If an account exists, you will receive a reset link shortly.")
39
- } catch (err) {
40
- if (isNextRedirect(err)) throw err
41
- setError(safeErrorMessage(err, "Something went wrong"))
42
- } finally {
43
- setPending(false)
44
- }
45
- }
46
-
47
- if (success) {
48
- return (
49
- <div className="space-y-5 text-center py-4">
50
- <Ornament />
51
- <div className="w-14 h-14 mx-auto rounded-full bg-brand-accent/10 flex items-center justify-center text-brand-accent">
52
- <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
53
- <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" />
54
- <polyline points="22,6 12,13 2,6" />
55
- </svg>
56
- </div>
57
- <h2 className={`${auth.title} text-xl`}>Check your email</h2>
58
- <p className="text-sm text-muted">{message}</p>
59
- <button type="button" onClick={onBack} className={auth.btnOutline}>
60
- Back to sign in
61
- </button>
62
- </div>
63
- )
64
- }
65
-
66
- return (
67
- <form onSubmit={handleSubmit} className="space-y-5">
68
- <div className="text-center lg:text-left">
69
- <Ornament />
70
- <h2 className={`${auth.title} text-xl lg:text-2xl`}>Reset password</h2>
71
- <p className="mt-2 text-sm text-muted">
72
- Enter your email and we&apos;ll send a link to reset your password.
73
- </p>
74
- </div>
75
-
76
- <AuthField
77
- label="Email"
78
- name="email"
79
- type="email"
80
- value={email}
81
- onChange={(e) => setEmail(e.target.value)}
82
- autoComplete="email"
83
- placeholder="you@example.com"
84
- />
85
-
86
- {error && (
87
- <p className="text-sm text-brand-sale" role="alert">
88
- {error}
89
- </p>
90
- )}
91
-
92
- <button
93
- type="submit"
94
- disabled={pending}
95
- className={`${auth.btnPrimary} ${auth.btnShine}`}
96
- >
97
- <span className="relative z-[1]">{pending ? "Sending…" : "Send reset link"}</span>
98
- </button>
99
-
100
- <button
101
- type="button"
102
- onClick={onBack}
103
- className="text-sm text-muted hover:text-brand-accent w-full text-center transition-colors"
104
- >
105
- Back to sign in
106
- </button>
107
- </form>
108
- )
109
- }
1
+ "use client"
2
+
3
+ import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
4
+ import { useState } from "react"
5
+ import { checkEmailRegistered, requestPasswordReset } from "./auth-server"
6
+ import { auth } from "./account-theme"
7
+ import { AuthField, Ornament } from "./login-form"
8
+
9
+ export default function ForgotPasswordForm({ onBack }: { onBack: () => void }) {
10
+ const [email, setEmail] = useState("")
11
+ const [message, setMessage] = useState<string | null>(null)
12
+ const [error, setError] = useState<string | null>(null)
13
+ const [pending, setPending] = useState(false)
14
+ const [success, setSuccess] = useState(false)
15
+
16
+ async function handleSubmit(e: React.FormEvent) {
17
+ e.preventDefault()
18
+ if (!email.trim()) return
19
+
20
+ setPending(true)
21
+ setMessage(null)
22
+ setError(null)
23
+
24
+ try {
25
+ const check = await checkEmailRegistered(email.trim())
26
+ if (!check.exists) {
27
+ setError("This email is not registered. Please create a new account instead.")
28
+ return
29
+ }
30
+
31
+ const result = await requestPasswordReset(email.trim())
32
+ if (!result.success) {
33
+ setError(result.error || "Failed to send reset link")
34
+ return
35
+ }
36
+
37
+ setSuccess(true)
38
+ setMessage(result.message || "If an account exists, you will receive a reset link shortly.")
39
+ } catch (err) {
40
+ if (isNextRedirect(err)) throw err
41
+ setError(safeErrorMessage(err, "Something went wrong"))
42
+ } finally {
43
+ setPending(false)
44
+ }
45
+ }
46
+
47
+ if (success) {
48
+ return (
49
+ <div className="space-y-5 text-center py-4">
50
+ <Ornament />
51
+ <div className="w-14 h-14 mx-auto rounded-full bg-brand-accent/10 flex items-center justify-center text-brand-accent">
52
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" aria-hidden>
53
+ <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" />
54
+ <polyline points="22,6 12,13 2,6" />
55
+ </svg>
56
+ </div>
57
+ <h2 className={`${auth.title} text-xl`}>Check your email</h2>
58
+ <p className="text-sm text-muted">{message}</p>
59
+ <button type="button" onClick={onBack} className={auth.btnOutline}>
60
+ Back to sign in
61
+ </button>
62
+ </div>
63
+ )
64
+ }
65
+
66
+ return (
67
+ <form onSubmit={handleSubmit} className="space-y-5">
68
+ <div className="text-center lg:text-left">
69
+ <Ornament />
70
+ <h2 className={`${auth.title} text-xl lg:text-2xl`}>Reset password</h2>
71
+ <p className="mt-2 text-sm text-muted">
72
+ Enter your email and we&apos;ll send a link to reset your password.
73
+ </p>
74
+ </div>
75
+
76
+ <AuthField
77
+ label="Email"
78
+ name="email"
79
+ type="email"
80
+ value={email}
81
+ onChange={(e) => setEmail(e.target.value)}
82
+ autoComplete="email"
83
+ placeholder="you@example.com"
84
+ />
85
+
86
+ {error && (
87
+ <p className="text-sm text-brand-sale" role="alert">
88
+ {error}
89
+ </p>
90
+ )}
91
+
92
+ <button
93
+ type="submit"
94
+ disabled={pending}
95
+ className={`${auth.btnPrimary} ${auth.btnShine}`}
96
+ >
97
+ <span className="relative z-[1]">{pending ? "Sending…" : "Send reset link"}</span>
98
+ </button>
99
+
100
+ <button
101
+ type="button"
102
+ onClick={onBack}
103
+ className="text-sm text-muted hover:text-brand-accent w-full text-center transition-colors"
104
+ >
105
+ Back to sign in
106
+ </button>
107
+ </form>
108
+ )
109
+ }
@@ -1,22 +1,22 @@
1
- "use client"
2
-
3
- import { GoogleSignInSection } from "./google-sign-in-section"
4
- import { auth } from "./account-theme"
5
-
6
- export default function GoogleAuthSection({
7
- countryCode,
8
- enableOneTap = true,
9
- }: {
10
- countryCode: string
11
- enableOneTap?: boolean
12
- }) {
13
- return (
14
- <GoogleSignInSection
15
- countryCode={countryCode}
16
- defaultCountryCode="in"
17
- enableOneTap={enableOneTap}
18
- credentialButtonClassName={`${auth.btnOutline} sr-google-btn`}
19
- oauthButtonClassName={`${auth.btnOutline} sr-google-btn`}
20
- />
21
- )
22
- }
1
+ "use client"
2
+
3
+ import { GoogleSignInSection } from "./google-sign-in-section"
4
+ import { auth } from "./account-theme"
5
+
6
+ export default function GoogleAuthSection({
7
+ countryCode,
8
+ enableOneTap = true,
9
+ }: {
10
+ countryCode: string
11
+ enableOneTap?: boolean
12
+ }) {
13
+ return (
14
+ <GoogleSignInSection
15
+ countryCode={countryCode}
16
+ defaultCountryCode="in"
17
+ enableOneTap={enableOneTap}
18
+ credentialButtonClassName={`${auth.btnOutline} sr-google-btn`}
19
+ oauthButtonClassName={`${auth.btnOutline} sr-google-btn`}
20
+ />
21
+ )
22
+ }
@@ -1,67 +1,67 @@
1
- "use client"
2
-
3
- import { useEffect, useState, type ReactNode } from "react"
4
- import { GoogleAuthProvider } from "@pradip1995/commerce-auth/components/google-oauth-provider"
5
- import { GoogleLoginButton } from "@pradip1995/commerce-auth/components/google-login"
6
- import { initiateGoogleAuth } from "./auth-server"
7
-
8
- /** commerce-auth GoogleAuthProvider wraps GoogleOAuthProvider only after mount; defer UI until then. */
9
- function DeferGoogleOAuthUi({ children }: { children: ReactNode }) {
10
- const [ready, setReady] = useState(false)
11
- useEffect(() => {
12
- setReady(true)
13
- }, [])
14
- if (!ready) return null
15
- return <>{children}</>
16
- }
17
-
18
- /**
19
- * Google Sign-In uses Medusa's authorization-code redirect.
20
- * The GIS credential button returns an ID token, which @medusajs/auth-google rejects.
21
- */
22
- export function GoogleSignInSection({
23
- countryCode,
24
- defaultCountryCode = "in",
25
- credentialButtonClassName,
26
- oauthButtonClassName,
27
- onError,
28
- }: {
29
- countryCode: string
30
- defaultCountryCode?: string
31
- enableOneTap?: boolean
32
- credentialButtonClassName?: string
33
- oauthButtonClassName?: string
34
- onError?: (message: string) => void
35
- }) {
36
- const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
37
- const [error, setError] = useState<string | null>(null)
38
-
39
- if (!clientId) return null
40
-
41
- return (
42
- <GoogleAuthProvider clientId={clientId}>
43
- <DeferGoogleOAuthUi>
44
- {error ? (
45
- <p className="mb-3 text-sm text-brand-sale" role="alert">
46
- {error}
47
- </p>
48
- ) : null}
49
- <div className="google-signin-section" data-gis="oauth">
50
- <GoogleLoginButton
51
- countryCode={countryCode}
52
- defaultCountryCode={defaultCountryCode}
53
- className={oauthButtonClassName || credentialButtonClassName}
54
- initiateAuth={async (countryCode?: string) => {
55
- const result = await initiateGoogleAuth(countryCode)
56
- if (result.error) {
57
- setError(result.error)
58
- onError?.(result.error)
59
- }
60
- return result
61
- }}
62
- />
63
- </div>
64
- </DeferGoogleOAuthUi>
65
- </GoogleAuthProvider>
66
- )
67
- }
1
+ "use client"
2
+
3
+ import { useEffect, useState, type ReactNode } from "react"
4
+ import { GoogleAuthProvider } from "@pradip1995/commerce-auth/components/google-oauth-provider"
5
+ import { GoogleLoginButton } from "@pradip1995/commerce-auth/components/google-login"
6
+ import { initiateGoogleAuth } from "./auth-server"
7
+
8
+ /** commerce-auth GoogleAuthProvider wraps GoogleOAuthProvider only after mount; defer UI until then. */
9
+ function DeferGoogleOAuthUi({ children }: { children: ReactNode }) {
10
+ const [ready, setReady] = useState(false)
11
+ useEffect(() => {
12
+ setReady(true)
13
+ }, [])
14
+ if (!ready) return null
15
+ return <>{children}</>
16
+ }
17
+
18
+ /**
19
+ * Google Sign-In uses Medusa's authorization-code redirect.
20
+ * The GIS credential button returns an ID token, which @medusajs/auth-google rejects.
21
+ */
22
+ export function GoogleSignInSection({
23
+ countryCode,
24
+ defaultCountryCode = "in",
25
+ credentialButtonClassName,
26
+ oauthButtonClassName,
27
+ onError,
28
+ }: {
29
+ countryCode: string
30
+ defaultCountryCode?: string
31
+ enableOneTap?: boolean
32
+ credentialButtonClassName?: string
33
+ oauthButtonClassName?: string
34
+ onError?: (message: string) => void
35
+ }) {
36
+ const clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID?.trim()
37
+ const [error, setError] = useState<string | null>(null)
38
+
39
+ if (!clientId) return null
40
+
41
+ return (
42
+ <GoogleAuthProvider clientId={clientId}>
43
+ <DeferGoogleOAuthUi>
44
+ {error ? (
45
+ <p className="mb-3 text-sm text-brand-sale" role="alert">
46
+ {error}
47
+ </p>
48
+ ) : null}
49
+ <div className="google-signin-section" data-gis="oauth">
50
+ <GoogleLoginButton
51
+ countryCode={countryCode}
52
+ defaultCountryCode={defaultCountryCode}
53
+ className={oauthButtonClassName || credentialButtonClassName}
54
+ initiateAuth={async (countryCode?: string) => {
55
+ const result = await initiateGoogleAuth(countryCode)
56
+ if (result.error) {
57
+ setError(result.error)
58
+ onError?.(result.error)
59
+ }
60
+ return result
61
+ }}
62
+ />
63
+ </div>
64
+ </DeferGoogleOAuthUi>
65
+ </GoogleAuthProvider>
66
+ )
67
+ }