@pradip1995/segment-login-template 0.5.24 → 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.
- package/package.json +54 -54
- package/src/account-addresses.tsx +287 -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/auth-server.ts +442 -442
- package/src/commerce-auth-otp-modal.css +125 -125
- package/src/commerce-auth-otp-modal.tsx +121 -121
- package/src/forgot-password-form.tsx +109 -109
- package/src/google-auth-section.tsx +22 -22
- package/src/google-sign-in-section.tsx +67 -142
- 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 +158 -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 +101 -101
- package/src/reset-password-form.tsx +103 -103
- package/src/reset-password-page.tsx +12 -12
- package/src/segment.tsx +272 -272
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import type { HttpTypes } from "@medusajs/types"
|
|
2
|
-
import {
|
|
3
|
-
deleteLineItem,
|
|
4
|
-
retrieveCart,
|
|
5
|
-
updateAddressSilently,
|
|
6
|
-
} from "@pradip1995/commerce-core/client/actions/cart"
|
|
7
|
-
import { retrieveCustomer } from "@pradip1995/commerce-core/data/customer"
|
|
8
|
-
import { retrieveOrder } from "@pradip1995/commerce-core/data/orders"
|
|
9
|
-
|
|
10
|
-
type AddressLike = {
|
|
11
|
-
first_name?: string | null
|
|
12
|
-
last_name?: string | null
|
|
13
|
-
address_1?: string | null
|
|
14
|
-
address_2?: string | null
|
|
15
|
-
city?: string | null
|
|
16
|
-
country_code?: string | null
|
|
17
|
-
postal_code?: string | null
|
|
18
|
-
province?: string | null
|
|
19
|
-
phone?: string | null
|
|
20
|
-
company?: string | null
|
|
21
|
-
is_default_shipping?: boolean | null
|
|
22
|
-
is_default_billing?: boolean | null
|
|
23
|
-
metadata?: Record<string, unknown> | null
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function pickDefaultCustomerAddress(
|
|
27
|
-
customer: HttpTypes.StoreCustomer | null | undefined
|
|
28
|
-
): AddressLike | undefined {
|
|
29
|
-
const addresses = (customer?.addresses ?? []) as AddressLike[]
|
|
30
|
-
return (
|
|
31
|
-
addresses.find((address) => address.is_default_shipping) ||
|
|
32
|
-
addresses.find(
|
|
33
|
-
(address) =>
|
|
34
|
-
address.metadata?.is_default === true || address.metadata?.is_default === "true"
|
|
35
|
-
) ||
|
|
36
|
-
addresses.find((address) => address.is_default_billing)
|
|
37
|
-
)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function hasUsableAddress(address?: AddressLike | null): address is AddressLike {
|
|
41
|
-
return !!(address && (address.address_1?.trim() || address.postal_code?.trim()))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function toCartAddress(source: AddressLike) {
|
|
45
|
-
return {
|
|
46
|
-
first_name: source.first_name || undefined,
|
|
47
|
-
last_name: source.last_name || undefined,
|
|
48
|
-
address_1: source.address_1 || undefined,
|
|
49
|
-
address_2: source.address_2 || undefined,
|
|
50
|
-
city: source.city || undefined,
|
|
51
|
-
country_code: source.country_code || undefined,
|
|
52
|
-
postal_code: source.postal_code || undefined,
|
|
53
|
-
province: source.province || undefined,
|
|
54
|
-
phone: source.phone || undefined,
|
|
55
|
-
company: source.company || undefined,
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Clear existing bag so each reorder starts from the order qty, not stacked qty. */
|
|
60
|
-
export async function clearCartLineItems() {
|
|
61
|
-
const cart = await retrieveCart().catch(() => null)
|
|
62
|
-
const items = cart?.items || []
|
|
63
|
-
for (const item of items) {
|
|
64
|
-
if (!item.id) continue
|
|
65
|
-
await deleteLineItem(item.id)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Prefill checkout cart address after reorder:
|
|
71
|
-
* 1) customer default address, else
|
|
72
|
-
* 2) shipping/billing from the reordered order.
|
|
73
|
-
*/
|
|
74
|
-
export async function applyReorderCheckoutAddress(order: HttpTypes.StoreOrder) {
|
|
75
|
-
try {
|
|
76
|
-
const customer = await retrieveCustomer().catch(() => null)
|
|
77
|
-
if (customer?.id === "pending_deletion") return
|
|
78
|
-
|
|
79
|
-
let source = pickDefaultCustomerAddress(customer)
|
|
80
|
-
|
|
81
|
-
if (!hasUsableAddress(source)) {
|
|
82
|
-
source = order.shipping_address || order.billing_address
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!hasUsableAddress(source) && order.id) {
|
|
86
|
-
const fullOrder = await retrieveOrder(order.id).catch(() => null)
|
|
87
|
-
source = fullOrder?.shipping_address || fullOrder?.billing_address
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (!hasUsableAddress(source)) return
|
|
91
|
-
|
|
92
|
-
await updateAddressSilently({
|
|
93
|
-
email: order.email || customer?.email || undefined,
|
|
94
|
-
shipping_address: toCartAddress(source),
|
|
95
|
-
same_as_billing: true,
|
|
96
|
-
assignShipping: true,
|
|
97
|
-
})
|
|
98
|
-
} catch {
|
|
99
|
-
// Checkout can still open; user can fill address manually.
|
|
100
|
-
}
|
|
101
|
-
}
|
|
1
|
+
import type { HttpTypes } from "@medusajs/types"
|
|
2
|
+
import {
|
|
3
|
+
deleteLineItem,
|
|
4
|
+
retrieveCart,
|
|
5
|
+
updateAddressSilently,
|
|
6
|
+
} from "@pradip1995/commerce-core/client/actions/cart"
|
|
7
|
+
import { retrieveCustomer } from "@pradip1995/commerce-core/data/customer"
|
|
8
|
+
import { retrieveOrder } from "@pradip1995/commerce-core/data/orders"
|
|
9
|
+
|
|
10
|
+
type AddressLike = {
|
|
11
|
+
first_name?: string | null
|
|
12
|
+
last_name?: string | null
|
|
13
|
+
address_1?: string | null
|
|
14
|
+
address_2?: string | null
|
|
15
|
+
city?: string | null
|
|
16
|
+
country_code?: string | null
|
|
17
|
+
postal_code?: string | null
|
|
18
|
+
province?: string | null
|
|
19
|
+
phone?: string | null
|
|
20
|
+
company?: string | null
|
|
21
|
+
is_default_shipping?: boolean | null
|
|
22
|
+
is_default_billing?: boolean | null
|
|
23
|
+
metadata?: Record<string, unknown> | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function pickDefaultCustomerAddress(
|
|
27
|
+
customer: HttpTypes.StoreCustomer | null | undefined
|
|
28
|
+
): AddressLike | undefined {
|
|
29
|
+
const addresses = (customer?.addresses ?? []) as AddressLike[]
|
|
30
|
+
return (
|
|
31
|
+
addresses.find((address) => address.is_default_shipping) ||
|
|
32
|
+
addresses.find(
|
|
33
|
+
(address) =>
|
|
34
|
+
address.metadata?.is_default === true || address.metadata?.is_default === "true"
|
|
35
|
+
) ||
|
|
36
|
+
addresses.find((address) => address.is_default_billing)
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hasUsableAddress(address?: AddressLike | null): address is AddressLike {
|
|
41
|
+
return !!(address && (address.address_1?.trim() || address.postal_code?.trim()))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function toCartAddress(source: AddressLike) {
|
|
45
|
+
return {
|
|
46
|
+
first_name: source.first_name || undefined,
|
|
47
|
+
last_name: source.last_name || undefined,
|
|
48
|
+
address_1: source.address_1 || undefined,
|
|
49
|
+
address_2: source.address_2 || undefined,
|
|
50
|
+
city: source.city || undefined,
|
|
51
|
+
country_code: source.country_code || undefined,
|
|
52
|
+
postal_code: source.postal_code || undefined,
|
|
53
|
+
province: source.province || undefined,
|
|
54
|
+
phone: source.phone || undefined,
|
|
55
|
+
company: source.company || undefined,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Clear existing bag so each reorder starts from the order qty, not stacked qty. */
|
|
60
|
+
export async function clearCartLineItems() {
|
|
61
|
+
const cart = await retrieveCart().catch(() => null)
|
|
62
|
+
const items = cart?.items || []
|
|
63
|
+
for (const item of items) {
|
|
64
|
+
if (!item.id) continue
|
|
65
|
+
await deleteLineItem(item.id)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Prefill checkout cart address after reorder:
|
|
71
|
+
* 1) customer default address, else
|
|
72
|
+
* 2) shipping/billing from the reordered order.
|
|
73
|
+
*/
|
|
74
|
+
export async function applyReorderCheckoutAddress(order: HttpTypes.StoreOrder) {
|
|
75
|
+
try {
|
|
76
|
+
const customer = await retrieveCustomer().catch(() => null)
|
|
77
|
+
if (customer?.id === "pending_deletion") return
|
|
78
|
+
|
|
79
|
+
let source = pickDefaultCustomerAddress(customer)
|
|
80
|
+
|
|
81
|
+
if (!hasUsableAddress(source)) {
|
|
82
|
+
source = order.shipping_address || order.billing_address
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!hasUsableAddress(source) && order.id) {
|
|
86
|
+
const fullOrder = await retrieveOrder(order.id).catch(() => null)
|
|
87
|
+
source = fullOrder?.shipping_address || fullOrder?.billing_address
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (!hasUsableAddress(source)) return
|
|
91
|
+
|
|
92
|
+
await updateAddressSilently({
|
|
93
|
+
email: order.email || customer?.email || undefined,
|
|
94
|
+
shipping_address: toCartAddress(source),
|
|
95
|
+
same_as_billing: true,
|
|
96
|
+
assignShipping: true,
|
|
97
|
+
})
|
|
98
|
+
} catch {
|
|
99
|
+
// Checkout can still open; user can fill address manually.
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -1,103 +1,103 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
-
import { useState } from "react"
|
|
5
|
-
import { useSearchParams } from "next/navigation"
|
|
6
|
-
import { completePasswordResetAction } from "./auth-server"
|
|
7
|
-
|
|
8
|
-
export default function ResetPasswordForm({ countryCode }: { countryCode: string }) {
|
|
9
|
-
const searchParams = useSearchParams()
|
|
10
|
-
const token = searchParams.get("token") || ""
|
|
11
|
-
const email = searchParams.get("email") || ""
|
|
12
|
-
|
|
13
|
-
const [password, setPassword] = useState("")
|
|
14
|
-
const [confirmPassword, setConfirmPassword] = useState("")
|
|
15
|
-
const [error, setError] = useState<string | null>(null)
|
|
16
|
-
const [pending, setPending] = useState(false)
|
|
17
|
-
|
|
18
|
-
async function handleSubmit(e: React.FormEvent) {
|
|
19
|
-
e.preventDefault()
|
|
20
|
-
setError(null)
|
|
21
|
-
|
|
22
|
-
if (!token || !email) {
|
|
23
|
-
setError("This reset link is invalid or expired. Request a new one from the sign-in page.")
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (password.length < 8) {
|
|
28
|
-
setError("Password must be at least 8 characters.")
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (password !== confirmPassword) {
|
|
33
|
-
setError("Passwords do not match.")
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
setPending(true)
|
|
38
|
-
|
|
39
|
-
try {
|
|
40
|
-
const result = await completePasswordResetAction({
|
|
41
|
-
email,
|
|
42
|
-
password,
|
|
43
|
-
token,
|
|
44
|
-
countryCode,
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
if (result && !result.success) {
|
|
48
|
-
setError(result.error || "Failed to reset password")
|
|
49
|
-
setPending(false)
|
|
50
|
-
}
|
|
51
|
-
} catch (err) {
|
|
52
|
-
if (isNextRedirect(err)) throw err
|
|
53
|
-
setError(safeErrorMessage(err, "Failed to reset password"))
|
|
54
|
-
setPending(false)
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return (
|
|
59
|
-
<div className="w-full max-w-md mx-auto space-y-6">
|
|
60
|
-
<div className="text-center space-y-2">
|
|
61
|
-
<h1 className="section-heading text-xl">Set a new password</h1>
|
|
62
|
-
<p className="text-sm text-muted">Choose a strong password for {email || "your account"}.</p>
|
|
63
|
-
</div>
|
|
64
|
-
|
|
65
|
-
<form onSubmit={handleSubmit} className="space-y-4">
|
|
66
|
-
<label className="block">
|
|
67
|
-
<span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
|
|
68
|
-
New password
|
|
69
|
-
</span>
|
|
70
|
-
<input
|
|
71
|
-
type="password"
|
|
72
|
-
value={password}
|
|
73
|
-
onChange={(e) => setPassword(e.target.value)}
|
|
74
|
-
required
|
|
75
|
-
autoComplete="new-password"
|
|
76
|
-
className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
|
|
77
|
-
/>
|
|
78
|
-
</label>
|
|
79
|
-
|
|
80
|
-
<label className="block">
|
|
81
|
-
<span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
|
|
82
|
-
Confirm password
|
|
83
|
-
</span>
|
|
84
|
-
<input
|
|
85
|
-
type="password"
|
|
86
|
-
value={confirmPassword}
|
|
87
|
-
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
88
|
-
required
|
|
89
|
-
autoComplete="new-password"
|
|
90
|
-
className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
|
|
91
|
-
/>
|
|
92
|
-
</label>
|
|
93
|
-
|
|
94
|
-
{error && <p className="text-sm text-brand-sale">{error}</p>}
|
|
95
|
-
|
|
96
|
-
<button type="submit" disabled={pending} className="btn-primary w-full disabled:opacity-60">
|
|
97
|
-
{pending ? "Updating…" : "Update password"}
|
|
98
|
-
</button>
|
|
99
|
-
</form>
|
|
100
|
-
</div>
|
|
101
|
-
)
|
|
102
|
-
}
|
|
103
|
-
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { isNextRedirect, safeErrorMessage } from "./is-next-redirect"
|
|
4
|
+
import { useState } from "react"
|
|
5
|
+
import { useSearchParams } from "next/navigation"
|
|
6
|
+
import { completePasswordResetAction } from "./auth-server"
|
|
7
|
+
|
|
8
|
+
export default function ResetPasswordForm({ countryCode }: { countryCode: string }) {
|
|
9
|
+
const searchParams = useSearchParams()
|
|
10
|
+
const token = searchParams.get("token") || ""
|
|
11
|
+
const email = searchParams.get("email") || ""
|
|
12
|
+
|
|
13
|
+
const [password, setPassword] = useState("")
|
|
14
|
+
const [confirmPassword, setConfirmPassword] = useState("")
|
|
15
|
+
const [error, setError] = useState<string | null>(null)
|
|
16
|
+
const [pending, setPending] = useState(false)
|
|
17
|
+
|
|
18
|
+
async function handleSubmit(e: React.FormEvent) {
|
|
19
|
+
e.preventDefault()
|
|
20
|
+
setError(null)
|
|
21
|
+
|
|
22
|
+
if (!token || !email) {
|
|
23
|
+
setError("This reset link is invalid or expired. Request a new one from the sign-in page.")
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (password.length < 8) {
|
|
28
|
+
setError("Password must be at least 8 characters.")
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (password !== confirmPassword) {
|
|
33
|
+
setError("Passwords do not match.")
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setPending(true)
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const result = await completePasswordResetAction({
|
|
41
|
+
email,
|
|
42
|
+
password,
|
|
43
|
+
token,
|
|
44
|
+
countryCode,
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
if (result && !result.success) {
|
|
48
|
+
setError(result.error || "Failed to reset password")
|
|
49
|
+
setPending(false)
|
|
50
|
+
}
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (isNextRedirect(err)) throw err
|
|
53
|
+
setError(safeErrorMessage(err, "Failed to reset password"))
|
|
54
|
+
setPending(false)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div className="w-full max-w-md mx-auto space-y-6">
|
|
60
|
+
<div className="text-center space-y-2">
|
|
61
|
+
<h1 className="section-heading text-xl">Set a new password</h1>
|
|
62
|
+
<p className="text-sm text-muted">Choose a strong password for {email || "your account"}.</p>
|
|
63
|
+
</div>
|
|
64
|
+
|
|
65
|
+
<form onSubmit={handleSubmit} className="space-y-4">
|
|
66
|
+
<label className="block">
|
|
67
|
+
<span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
|
|
68
|
+
New password
|
|
69
|
+
</span>
|
|
70
|
+
<input
|
|
71
|
+
type="password"
|
|
72
|
+
value={password}
|
|
73
|
+
onChange={(e) => setPassword(e.target.value)}
|
|
74
|
+
required
|
|
75
|
+
autoComplete="new-password"
|
|
76
|
+
className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
|
|
77
|
+
/>
|
|
78
|
+
</label>
|
|
79
|
+
|
|
80
|
+
<label className="block">
|
|
81
|
+
<span className="text-xs font-semibold uppercase tracking-[var(--letter-spacing-nav)] text-muted mb-1.5 block">
|
|
82
|
+
Confirm password
|
|
83
|
+
</span>
|
|
84
|
+
<input
|
|
85
|
+
type="password"
|
|
86
|
+
value={confirmPassword}
|
|
87
|
+
onChange={(e) => setConfirmPassword(e.target.value)}
|
|
88
|
+
required
|
|
89
|
+
autoComplete="new-password"
|
|
90
|
+
className="w-full border border-cart-border rounded px-3 py-2.5 text-sm bg-surface focus:outline-none focus:border-brand-accent"
|
|
91
|
+
/>
|
|
92
|
+
</label>
|
|
93
|
+
|
|
94
|
+
{error && <p className="text-sm text-brand-sale">{error}</p>}
|
|
95
|
+
|
|
96
|
+
<button type="submit" disabled={pending} className="btn-primary w-full disabled:opacity-60">
|
|
97
|
+
{pending ? "Updating…" : "Update password"}
|
|
98
|
+
</button>
|
|
99
|
+
</form>
|
|
100
|
+
</div>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { Suspense } from "react"
|
|
4
|
-
import ResetPasswordForm from "./reset-password-form"
|
|
5
|
-
|
|
6
|
-
export default function ResetPasswordPage({ countryCode = "in" }: { countryCode?: string }) {
|
|
7
|
-
return (
|
|
8
|
-
<Suspense fallback={<p className="text-sm text-muted text-center py-8">Loading…</p>}>
|
|
9
|
-
<ResetPasswordForm countryCode={countryCode} />
|
|
10
|
-
</Suspense>
|
|
11
|
-
)
|
|
12
|
-
}
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { Suspense } from "react"
|
|
4
|
+
import ResetPasswordForm from "./reset-password-form"
|
|
5
|
+
|
|
6
|
+
export default function ResetPasswordPage({ countryCode = "in" }: { countryCode?: string }) {
|
|
7
|
+
return (
|
|
8
|
+
<Suspense fallback={<p className="text-sm text-muted text-center py-8">Loading…</p>}>
|
|
9
|
+
<ResetPasswordForm countryCode={countryCode} />
|
|
10
|
+
</Suspense>
|
|
11
|
+
)
|
|
12
|
+
}
|