medusa-storefront-data 1.0.0
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/dist/config.d.ts +3 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +31 -0
- package/dist/cookies.d.ts +23 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +140 -0
- package/dist/server/cart.d.ts +92 -0
- package/dist/server/cart.d.ts.map +1 -0
- package/dist/server/cart.js +827 -0
- package/dist/server/categories.d.ts +3 -0
- package/dist/server/categories.d.ts.map +1 -0
- package/dist/server/categories.js +71 -0
- package/dist/server/collections.d.ts +8 -0
- package/dist/server/collections.d.ts.map +1 -0
- package/dist/server/collections.js +84 -0
- package/dist/server/customer-registration.d.ts +142 -0
- package/dist/server/customer-registration.d.ts.map +1 -0
- package/dist/server/customer-registration.js +295 -0
- package/dist/server/customer.d.ts +48 -0
- package/dist/server/customer.d.ts.map +1 -0
- package/dist/server/customer.js +462 -0
- package/dist/server/dynamic-config.d.ts +125 -0
- package/dist/server/dynamic-config.d.ts.map +1 -0
- package/dist/server/dynamic-config.js +263 -0
- package/dist/server/fulfillment.d.ts +4 -0
- package/dist/server/fulfillment.d.ts.map +1 -0
- package/dist/server/fulfillment.js +72 -0
- package/dist/server/guest.d.ts +109 -0
- package/dist/server/guest.d.ts.map +1 -0
- package/dist/server/guest.js +304 -0
- package/dist/server/index.d.ts +21 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +20 -0
- package/dist/server/locale-actions.d.ts +14 -0
- package/dist/server/locale-actions.d.ts.map +1 -0
- package/dist/server/locale-actions.js +63 -0
- package/dist/server/locales.d.ts +10 -0
- package/dist/server/locales.d.ts.map +1 -0
- package/dist/server/locales.js +20 -0
- package/dist/server/notifications.d.ts +2 -0
- package/dist/server/notifications.d.ts.map +1 -0
- package/dist/server/notifications.js +20 -0
- package/dist/server/onboarding.d.ts +2 -0
- package/dist/server/onboarding.d.ts.map +1 -0
- package/dist/server/onboarding.js +8 -0
- package/dist/server/orders.d.ts +69 -0
- package/dist/server/orders.d.ts.map +1 -0
- package/dist/server/orders.js +371 -0
- package/dist/server/payment-details.d.ts +5 -0
- package/dist/server/payment-details.d.ts.map +1 -0
- package/dist/server/payment-details.js +53 -0
- package/dist/server/payment.d.ts +2 -0
- package/dist/server/payment.d.ts.map +1 -0
- package/dist/server/payment.js +25 -0
- package/dist/server/products.d.ts +58 -0
- package/dist/server/products.d.ts.map +1 -0
- package/dist/server/products.js +285 -0
- package/dist/server/regions.d.ts +5 -0
- package/dist/server/regions.d.ts.map +1 -0
- package/dist/server/regions.js +54 -0
- package/dist/server/returns.d.ts +29 -0
- package/dist/server/returns.d.ts.map +1 -0
- package/dist/server/returns.js +236 -0
- package/dist/server/swaps.d.ts +14 -0
- package/dist/server/swaps.d.ts.map +1 -0
- package/dist/server/swaps.js +123 -0
- package/dist/server/variants.d.ts +3 -0
- package/dist/server/variants.d.ts.map +1 -0
- package/dist/server/variants.js +26 -0
- package/dist/util/get-locale-header.d.ts +4 -0
- package/dist/util/get-locale-header.d.ts.map +1 -0
- package/dist/util/get-locale-header.js +7 -0
- package/dist/util/medusa-error.d.ts +2 -0
- package/dist/util/medusa-error.d.ts.map +1 -0
- package/dist/util/medusa-error.js +18 -0
- package/package.json +152 -0
- package/src/config.ts +39 -0
- package/src/cookies.ts +171 -0
- package/src/middleware.ts +2 -0
- package/src/server/cart.ts +1054 -0
- package/src/server/categories.ts +94 -0
- package/src/server/collections.ts +113 -0
- package/src/server/customer-registration.ts +349 -0
- package/src/server/customer.ts +581 -0
- package/src/server/dynamic-config.ts +403 -0
- package/src/server/fulfillment.ts +97 -0
- package/src/server/guest.ts +333 -0
- package/src/server/index.ts +21 -0
- package/src/server/locale-actions.ts +74 -0
- package/src/server/locales.ts +28 -0
- package/src/server/notifications.ts +22 -0
- package/src/server/onboarding.ts +9 -0
- package/src/server/orders.ts +467 -0
- package/src/server/payment-details.ts +69 -0
- package/src/server/payment.ts +35 -0
- package/src/server/products.ts +378 -0
- package/src/server/regions.ts +66 -0
- package/src/server/returns.ts +294 -0
- package/src/server/swaps.ts +150 -0
- package/src/server/variants.ts +38 -0
- package/src/server/wishlist.ts +64 -0
- package/src/services/middleware.ts +54 -0
- package/src/util/get-locale-header.ts +8 -0
- package/src/util/medusa-error.ts +19 -0
- package/src/util/sort-products.ts +47 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { sdk } from "../config"
|
|
2
|
+
import { HttpTypes } from "@medusajs/types"
|
|
3
|
+
import { getCacheOptions } from "../cookies"
|
|
4
|
+
|
|
5
|
+
export const listCategories = async (query?: Record<string, any>) => {
|
|
6
|
+
try {
|
|
7
|
+
const next = {
|
|
8
|
+
...(await getCacheOptions("categories")),
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const limit = query?.limit || 100
|
|
12
|
+
|
|
13
|
+
return sdk.client
|
|
14
|
+
.fetch<{ product_categories: HttpTypes.StoreProductCategory[] }>(
|
|
15
|
+
"/store/product-categories",
|
|
16
|
+
{
|
|
17
|
+
query: {
|
|
18
|
+
fields:
|
|
19
|
+
"*category_children, *products, *parent_category, *parent_category.parent_category",
|
|
20
|
+
limit,
|
|
21
|
+
...query,
|
|
22
|
+
},
|
|
23
|
+
next: {
|
|
24
|
+
...next,
|
|
25
|
+
revalidate: 0,
|
|
26
|
+
},
|
|
27
|
+
cache: "no-store",
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
.then(({ product_categories }) => product_categories)
|
|
31
|
+
.catch((error) => {
|
|
32
|
+
if (process.env.NODE_ENV === 'production' || process.env.CI) {
|
|
33
|
+
return []
|
|
34
|
+
}
|
|
35
|
+
throw error
|
|
36
|
+
})
|
|
37
|
+
} catch (error: any) {
|
|
38
|
+
if (process.env.NODE_ENV === 'production' || process.env.CI) {
|
|
39
|
+
return []
|
|
40
|
+
}
|
|
41
|
+
throw error
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const getCategoryByHandle = async (categoryHandle: string[]) => {
|
|
46
|
+
try {
|
|
47
|
+
const handle = `${categoryHandle.join("/")}`
|
|
48
|
+
const decodedHandle = decodeURIComponent(handle)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
const next = {
|
|
52
|
+
...(await getCacheOptions("categories")),
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const result = await sdk.client
|
|
56
|
+
.fetch<HttpTypes.StoreProductCategoryListResponse>(
|
|
57
|
+
`/store/product-categories`,
|
|
58
|
+
{
|
|
59
|
+
query: {
|
|
60
|
+
handle: [decodedHandle],
|
|
61
|
+
},
|
|
62
|
+
next: {
|
|
63
|
+
...next,
|
|
64
|
+
revalidate: 0,
|
|
65
|
+
},
|
|
66
|
+
cache: "no-store",
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
if (result.product_categories && result.product_categories.length > 0) {
|
|
71
|
+
return result.product_categories[0]
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Fallback: Fetch all categories and find manually
|
|
75
|
+
|
|
76
|
+
const allCategories = await listCategories({ limit: "200" })
|
|
77
|
+
const matchedCategory = allCategories.find(c =>
|
|
78
|
+
c.handle === decodedHandle ||
|
|
79
|
+
c.handle === handle ||
|
|
80
|
+
c.handle?.replace(/-/g, " ") === decodedHandle
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
if (matchedCategory) {
|
|
84
|
+
|
|
85
|
+
return matchedCategory
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
return null
|
|
90
|
+
} catch (error: any) {
|
|
91
|
+
|
|
92
|
+
return null
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use server"
|
|
2
|
+
|
|
3
|
+
import { sdk } from "../config"
|
|
4
|
+
import { HttpTypes } from "@medusajs/types"
|
|
5
|
+
import { getCacheOptions } from "../cookies"
|
|
6
|
+
|
|
7
|
+
export const retrieveCollection = async (id: string) => {
|
|
8
|
+
const next = {
|
|
9
|
+
...(await getCacheOptions("collections")),
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return sdk.client
|
|
13
|
+
.fetch<{ collection: HttpTypes.StoreCollection }>(
|
|
14
|
+
`/store/collections/${id}`,
|
|
15
|
+
{
|
|
16
|
+
next: {
|
|
17
|
+
...next,
|
|
18
|
+
revalidate: 0,
|
|
19
|
+
},
|
|
20
|
+
cache: "no-store",
|
|
21
|
+
}
|
|
22
|
+
)
|
|
23
|
+
.then(({ collection }) => collection)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const listCollections = async (
|
|
27
|
+
queryParams: Record<string, string> = {}
|
|
28
|
+
): Promise<{ collections: HttpTypes.StoreCollection[]; count: number }> => {
|
|
29
|
+
try {
|
|
30
|
+
const next = {
|
|
31
|
+
...(await getCacheOptions("collections")),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
queryParams.limit = queryParams.limit || "100"
|
|
35
|
+
queryParams.offset = queryParams.offset || "0"
|
|
36
|
+
|
|
37
|
+
return sdk.client
|
|
38
|
+
.fetch<{ collections: HttpTypes.StoreCollection[]; count: number }>(
|
|
39
|
+
"/store/collections",
|
|
40
|
+
{
|
|
41
|
+
query: queryParams,
|
|
42
|
+
next: {
|
|
43
|
+
...next,
|
|
44
|
+
revalidate: 0,
|
|
45
|
+
},
|
|
46
|
+
cache: "no-store",
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
.then(({ collections }) => ({ collections, count: collections.length }))
|
|
50
|
+
.catch((error) => {
|
|
51
|
+
if (process.env.NODE_ENV === 'production' || process.env.CI) {
|
|
52
|
+
return { collections: [], count: 0 }
|
|
53
|
+
}
|
|
54
|
+
throw error
|
|
55
|
+
})
|
|
56
|
+
} catch (error: any) {
|
|
57
|
+
if (process.env.NODE_ENV === 'production' || process.env.CI) {
|
|
58
|
+
return { collections: [], count: 0 }
|
|
59
|
+
}
|
|
60
|
+
throw error
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const getCollectionByHandle = async (
|
|
65
|
+
handle: string
|
|
66
|
+
): Promise<HttpTypes.StoreCollection | null> => {
|
|
67
|
+
try {
|
|
68
|
+
const next = {
|
|
69
|
+
...(await getCacheOptions("collections")),
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Ensure handle is decoded
|
|
73
|
+
const decodedHandle = decodeURIComponent(handle)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
// Try array-based handle filter (standard for v2)
|
|
77
|
+
const result = await sdk.client
|
|
78
|
+
.fetch<HttpTypes.StoreCollectionListResponse>(`/store/collections`, {
|
|
79
|
+
query: {
|
|
80
|
+
handle: [decodedHandle],
|
|
81
|
+
},
|
|
82
|
+
next: {
|
|
83
|
+
...next,
|
|
84
|
+
revalidate: 0,
|
|
85
|
+
},
|
|
86
|
+
cache: "no-store",
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
if (result.collections && result.collections.length > 0) {
|
|
90
|
+
return result.collections[0]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Fallback: Fetch all and find manually (useful for handles with spaces/special chars)
|
|
94
|
+
|
|
95
|
+
const { collections } = await listCollections({ limit: "100" })
|
|
96
|
+
const matchedCollection = collections.find(c =>
|
|
97
|
+
c.handle === decodedHandle ||
|
|
98
|
+
c.handle === handle ||
|
|
99
|
+
c.handle?.replace(/-/g, " ") === decodedHandle
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if (matchedCollection) {
|
|
103
|
+
|
|
104
|
+
return matchedCollection
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
return null
|
|
109
|
+
} catch (error: any) {
|
|
110
|
+
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"use server"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* API functions for customer-registration plugin
|
|
5
|
+
* Handles OTP-based email verification during signup
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { getAuthHeaders } from "../cookies"
|
|
9
|
+
|
|
10
|
+
const getHeaders = () => {
|
|
11
|
+
const publishableKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY
|
|
12
|
+
return {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
"x-publishable-api-key": publishableKey || "",
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getBaseUrl = () => {
|
|
19
|
+
return process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Register a new customer and trigger OTP email
|
|
24
|
+
* Uses standard Medusa customer creation endpoint
|
|
25
|
+
* Plugin automatically sends OTP when customer is created
|
|
26
|
+
*/
|
|
27
|
+
export async function registerCustomer(data: {
|
|
28
|
+
email: string
|
|
29
|
+
first_name: string
|
|
30
|
+
last_name: string
|
|
31
|
+
phone?: string
|
|
32
|
+
password: string
|
|
33
|
+
}) {
|
|
34
|
+
// Sanitize phone number if provided
|
|
35
|
+
if (data.phone) {
|
|
36
|
+
data.phone = data.phone.replace(/[^\d+]/g, "")
|
|
37
|
+
}
|
|
38
|
+
const baseUrl = getBaseUrl()
|
|
39
|
+
|
|
40
|
+
// Step 1: Get registration token from auth endpoint
|
|
41
|
+
const authResponse = await fetch(`${baseUrl}/auth/customer/emailpass/register`, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: getHeaders(),
|
|
44
|
+
body: JSON.stringify({
|
|
45
|
+
email: data.email,
|
|
46
|
+
password: data.password, // Use user's actual password
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
if (!authResponse.ok) {
|
|
51
|
+
const err = await authResponse.json().catch(() => ({}))
|
|
52
|
+
return { success: false, error: err.message || "Failed to initiate registration" }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const authData = await authResponse.json()
|
|
56
|
+
const registrationToken = authData.token
|
|
57
|
+
|
|
58
|
+
// Step 2: Create customer with the registration token
|
|
59
|
+
const customerResponse = await fetch(`${baseUrl}/store/customers`, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: {
|
|
62
|
+
...getHeaders(),
|
|
63
|
+
"Authorization": `Bearer ${registrationToken}`,
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify({
|
|
66
|
+
email: data.email,
|
|
67
|
+
first_name: data.first_name,
|
|
68
|
+
last_name: data.last_name,
|
|
69
|
+
phone: data.phone || undefined,
|
|
70
|
+
}),
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
if (!customerResponse.ok) {
|
|
74
|
+
const err = await customerResponse.json().catch(() => ({}))
|
|
75
|
+
return { success: false, error: err.message || "Failed to create customer" }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const customerData = await customerResponse.json()
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
success: true,
|
|
82
|
+
customer: customerData.customer,
|
|
83
|
+
message: "Registration initiated. Please check your email for OTP."
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Send OTP to customer for email verification
|
|
89
|
+
*/
|
|
90
|
+
export async function sendCustomerOTP(customerId: string, type: string = "email_verification") {
|
|
91
|
+
const baseUrl = getBaseUrl()
|
|
92
|
+
|
|
93
|
+
const response = await fetch(`${baseUrl}/store/customers/otp/send`, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: getHeaders(),
|
|
96
|
+
body: JSON.stringify({
|
|
97
|
+
customer_id: customerId,
|
|
98
|
+
type: type,
|
|
99
|
+
}),
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
const err = await response.json().catch(() => ({}))
|
|
104
|
+
return { success: false, error: err.message || "Failed to send OTP" }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const data = await response.json()
|
|
108
|
+
return {
|
|
109
|
+
success: true,
|
|
110
|
+
token: data.token,
|
|
111
|
+
message: data.message || "OTP sent successfully"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Verify OTP code
|
|
117
|
+
*/
|
|
118
|
+
export async function verifyCustomerOTP(token: string, code: string) {
|
|
119
|
+
const baseUrl = getBaseUrl()
|
|
120
|
+
|
|
121
|
+
const response = await fetch(`${baseUrl}/store/customers/otp/verify`, {
|
|
122
|
+
method: "POST",
|
|
123
|
+
headers: getHeaders(),
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
token: token,
|
|
126
|
+
code: code,
|
|
127
|
+
}),
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
if (!response.ok) {
|
|
131
|
+
const err = await response.json().catch(() => ({}))
|
|
132
|
+
return { success: false, error: err.message || "Invalid OTP" }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const data = await response.json()
|
|
136
|
+
return {
|
|
137
|
+
success: true,
|
|
138
|
+
verified: true,
|
|
139
|
+
customer: data.customer,
|
|
140
|
+
message: "Email verified successfully"
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Update customer password after email verification
|
|
146
|
+
* This allows user to set their own password
|
|
147
|
+
*/
|
|
148
|
+
export async function setCustomerPassword(email: string, password: string) {
|
|
149
|
+
const baseUrl = getBaseUrl()
|
|
150
|
+
|
|
151
|
+
// Use password reset flow to set password
|
|
152
|
+
// First request reset token
|
|
153
|
+
const resetResponse = await fetch(`${baseUrl}/auth/customer/emailpass/reset-password`, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: getHeaders(),
|
|
156
|
+
body: JSON.stringify({
|
|
157
|
+
identifier: email,
|
|
158
|
+
}),
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
// The reset request doesn't need to succeed for this flow
|
|
162
|
+
// We'll use direct login with the new password approach instead
|
|
163
|
+
|
|
164
|
+
// Actually for registration, we should update the auth identity password
|
|
165
|
+
// This might need a different approach based on how the plugin works
|
|
166
|
+
|
|
167
|
+
return { success: true, message: "Please use the password you entered during registration to login" }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Request a password reset email
|
|
172
|
+
* Calls the customer-registration plugin's reset-password endpoint
|
|
173
|
+
* Email with reset link will be sent to the user
|
|
174
|
+
*/
|
|
175
|
+
export async function requestPasswordReset(email: string) {
|
|
176
|
+
const baseUrl = getBaseUrl()
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
const response = await fetch(`${baseUrl}/auth/customer/emailpass/reset-password`, {
|
|
180
|
+
method: "POST",
|
|
181
|
+
headers: getHeaders(),
|
|
182
|
+
body: JSON.stringify({
|
|
183
|
+
email: email.toLowerCase().trim(),
|
|
184
|
+
}),
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// API returns 201 with empty object for security (doesn't reveal if email exists)
|
|
188
|
+
if (response.ok) {
|
|
189
|
+
return {
|
|
190
|
+
success: true,
|
|
191
|
+
message: "If an account exists with this email, you will receive a password reset link."
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const err = await response.json().catch(() => ({}))
|
|
196
|
+
return { success: false, error: err.message || "Failed to request password reset" }
|
|
197
|
+
} catch (error: any) {
|
|
198
|
+
return { success: false, error: error.message || "Network error occurred" }
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Complete password reset with new password
|
|
204
|
+
* Uses the token from the reset email link
|
|
205
|
+
*/
|
|
206
|
+
export async function completePasswordReset(data: {
|
|
207
|
+
email: string
|
|
208
|
+
password: string
|
|
209
|
+
token: string
|
|
210
|
+
}) {
|
|
211
|
+
const baseUrl = getBaseUrl()
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const response = await fetch(`${baseUrl}/auth/customer/emailpass/update`, {
|
|
215
|
+
method: "POST",
|
|
216
|
+
headers: {
|
|
217
|
+
...getHeaders(),
|
|
218
|
+
"Authorization": `Bearer ${data.token}`,
|
|
219
|
+
},
|
|
220
|
+
body: JSON.stringify({
|
|
221
|
+
email: data.email.toLowerCase().trim(),
|
|
222
|
+
password: data.password,
|
|
223
|
+
token: data.token,
|
|
224
|
+
}),
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
if (!response.ok) {
|
|
228
|
+
const err = await response.json().catch(() => ({}))
|
|
229
|
+
return { success: false, error: err.message || "Failed to reset password" }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const result = await response.json()
|
|
233
|
+
return {
|
|
234
|
+
success: true,
|
|
235
|
+
message: "Password reset successfully. You can now login with your new password."
|
|
236
|
+
}
|
|
237
|
+
} catch (error: any) {
|
|
238
|
+
return { success: false, error: error.message || "Network error occurred" }
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Check if a customer email is already registered
|
|
244
|
+
*/
|
|
245
|
+
export async function checkEmailRegistered(email: string) {
|
|
246
|
+
const baseUrl = getBaseUrl()
|
|
247
|
+
try {
|
|
248
|
+
const response = await fetch(`${baseUrl}/auth/customer/emailpass/register`, {
|
|
249
|
+
method: "POST",
|
|
250
|
+
headers: getHeaders(),
|
|
251
|
+
body: JSON.stringify({
|
|
252
|
+
email: email.toLowerCase().trim(),
|
|
253
|
+
password: "DUMMY_PASSWORD_CHECK_IGNORE",
|
|
254
|
+
}),
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
// If it returns 201 (Created), it means registration COULD start, so email doesn't exist yet
|
|
258
|
+
if (response.status === 201 || response.status === 200) {
|
|
259
|
+
return { exists: false }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// For any other status (like 400, 409, 422), we assume the identity already exists
|
|
263
|
+
// This prevents blocking registered users if the error message doesn't match exactly
|
|
264
|
+
return { exists: true }
|
|
265
|
+
} catch (e) {
|
|
266
|
+
// Fallback to true to avoid blocking the user if the network check fails
|
|
267
|
+
return { exists: true }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Request account deletion
|
|
272
|
+
*/
|
|
273
|
+
export async function requestAccountDeletion(email: string) {
|
|
274
|
+
const baseUrl = getBaseUrl()
|
|
275
|
+
const response = await fetch(`${baseUrl}/store/customers/account-deletion/request`, {
|
|
276
|
+
method: "POST",
|
|
277
|
+
headers: {
|
|
278
|
+
...getHeaders(),
|
|
279
|
+
...(await getAuthHeaders()),
|
|
280
|
+
},
|
|
281
|
+
body: JSON.stringify({ email }),
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
if (!response.ok) {
|
|
285
|
+
const err = await response.json().catch(() => ({}))
|
|
286
|
+
return { success: false, error: err.message || "Failed to request deletion" }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const data = await response.json()
|
|
290
|
+
return { success: true, token: data.token }
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Confirm account deletion with OTP
|
|
295
|
+
*/
|
|
296
|
+
export async function confirmAccountDeletion(token: string, code: string) {
|
|
297
|
+
const baseUrl = getBaseUrl()
|
|
298
|
+
const response = await fetch(`${baseUrl}/store/customers/account-deletion/confirm`, {
|
|
299
|
+
method: "POST",
|
|
300
|
+
headers: getHeaders(),
|
|
301
|
+
body: JSON.stringify({ token, code }),
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
if (!response.ok) {
|
|
305
|
+
const err = await response.json().catch(() => ({}))
|
|
306
|
+
return { success: false, error: err.message || "Invalid OTP" }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return { success: true }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Request cancellation of a pending account deletion
|
|
314
|
+
*/
|
|
315
|
+
export async function requestAccountDeletionCancel(email: string) {
|
|
316
|
+
const baseUrl = getBaseUrl()
|
|
317
|
+
const response = await fetch(`${baseUrl}/store/customers/account-deletion/cancel-request`, {
|
|
318
|
+
method: "POST",
|
|
319
|
+
headers: getHeaders(),
|
|
320
|
+
body: JSON.stringify({ email }),
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
if (!response.ok) {
|
|
324
|
+
const err = await response.json().catch(() => ({}))
|
|
325
|
+
return { success: false, error: err.message || "Failed to request cancellation" }
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const data = await response.json()
|
|
329
|
+
return { success: true, token: data.token }
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Confirm cancellation of account deletion with OTP
|
|
334
|
+
*/
|
|
335
|
+
export async function confirmAccountDeletionCancel(token: string, code: string) {
|
|
336
|
+
const baseUrl = getBaseUrl()
|
|
337
|
+
const response = await fetch(`${baseUrl}/store/customers/account-deletion/cancel-confirm`, {
|
|
338
|
+
method: "POST",
|
|
339
|
+
headers: getHeaders(),
|
|
340
|
+
body: JSON.stringify({ token, code }),
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
if (!response.ok) {
|
|
344
|
+
const err = await response.json().catch(() => ({}))
|
|
345
|
+
return { success: false, error: err.message || "Invalid OTP" }
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return { success: true }
|
|
349
|
+
}
|