1 ---
2 tags:
3 - customer
4 - storefront
5 - auth
6 products:
7 - customer
8 - auth
9 ---
10
11 import { CodeTabs, CodeTab, Prerequisites } from "docs-ui"
12
13 export const metadata = {
14 title: `Reset Customer Password in Storefront`,
15 }
16
17 # {metadata.title}
18
19 In this guide, you'll learn how to implement the flow to reset a customer's password in your storefront.
20
21 ## Reset Password Flow in Storefront
22
23 Customers need to reset their password if they forget it. To implement the flow to reset a customer's password, you need two pages in your storefront:
24
25 1. [Request Reset Password Page](#1-request-reset-password-page): A page to request the password reset.
26 - When the customer requests to reset their password, they would receive an email (or other notification) with a URL to the Reset Password Page.
27 2. [Reset Password Page](#2-reset-password-page): A page that prompts the customer to enter a new password.
28
29 <Prerequisites
30 items={[
31 {
32 text: "To send the customer an email (or other notification) with the URL to reset their password, you must implement the subscriber that handles the notification.",
33 link: "/commerce-modules/auth/reset-password"
34 }
35 ]}
36 />
37
38 ---
39
40 ## 1. Request Reset Password Page
41
42 The request password reset page prompts the customer to enter their email. Then, it sends a request to the [Request Reset Password Token API route](!api!/store/auth/generate-reset-password-token) to request resetting the password.
43
44 This API route will then handle sending an email or another type of notification, if you handle it as explained in the [Reset Password Guide](../../../commerce-modules/auth/reset-password/page.mdx).
45
46 For example, you can implement the following functionality in your storefront to request resetting the password:
47
48 <Note title="Tip">
49
50 Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
51
52 </Note>
53
54 <CodeTabs group="store-request">
55 <CodeTab label="React" value="react">
56
57 export const highlights = [
58 ["20", "resetPassword", "Request resetting the password."],
59 ["21", "identifier", "Pass the email in the `identifier` request body parameter."]
60 ]
61
62 ```tsx highlights={highlights}
63 "use client" // include with Next.js 13+
64
65 import { useState } from "react"
66 import { sdk } from "@/lib/sdk"
67
68 export default function RequestResetPassword() {
69 const [loading, setLoading] = useState(false)
70 const [email, setEmail] = useState("")
71
72 const handleSubmit = async (
73 e: React.FormEvent<HTMLFormElement>
74 ) => {
75 e.preventDefault()
76 if (!email) {
77 alert("Email is required")
78 return
79 }
80 setLoading(true)
81
82 sdk.auth.resetPassword("customer", "emailpass", {
83 identifier: email,
84 })
85 .then(() => {
86 alert("If an account exists with the specified email, it'll receive instructions to reset the password.")
87 })
88 .catch((error) => {
89 alert(error.message)
90 })
91 .finally(() => {
92 setLoading(false)
93 })
94 }
95
96 return (
97 <form onSubmit={handleSubmit}>
98 <label>Email</label>
99 <input
100 placeholder="Email"
101 type="email"
102 value={email}
103 onChange={(e) => setEmail(e.target.value)}
104 />
105 <button type="submit" disabled={loading}>
106 Request Password Reset
107 </button>
108 </form>
109 )
110 }
111 ```
112
113 </CodeTab>
114 <CodeTab label="JS SDK" value="js-sdk">
115
116 export const fetchHighlights = [
117 ["5", "email", "Assuming the email is retrieved from an input field."],
118 ["10", "resetPassword", "Request resetting the password."],
119 ["11", "identifier", "Pass the email in the `identifier` request body parameter."]
120 ]
121
122 ```ts highlights={fetchHighlights}
123 const handleSubmit = async (
124 e: React.FormEvent<HTMLFormElement> // or other form event
125 ) => {
126 e.preventDefault()
127 if (!email) {
128 alert("Email is required")
129 return
130 }
131
132 sdk.auth.resetPassword("customer", "emailpass", {
133 identifier: email,
134 })
135 .then(() => {
136 alert("If an account exists with the specified email, it'll receive instructions to reset the password.")
137 })
138 .catch((error) => {
139 alert(error.message)
140 })
141 }
142 ```
143
144 </CodeTab>
145 </CodeTabs>
146
147 In this example, you send a request to the [Request Reset Password Token API route](!api!/store/auth/generate-reset-password-token) when the form that has the email field is submitted.
148
149 In the request body, you pass an `identifier` parameter, which is the customer's email.
150
151 <Note title="Tip">
152
153 The [Request Reset Password Token API route](!api!/store/auth/generate-reset-password-token) returns a successful response always, even if the customer's email doesn't exist. This ensures that customer emails that don't exist are not exposed.
154
155 </Note>
156
157 ---
158
159 ## 2. Reset Password Page
160
161 Once the customer requests to reset their password, you should handle sending them a notification, such as an email, as explained in the [Reset Password Guide](../../../commerce-modules/auth/reset-password/page.mdx).
162
163 The notification should include a URL in your storefront that allows the customer to update their password. In this step, you'll implement this page.
164
165 The reset password page should receive a `token` and `email` query parameters. Then, it prompts the customer for a new password, and sends a request to the [Reset Password API route](!api!/store/auth/reset-password) to update the password.
166
167 <Note>
168
169 If you followed [this guide](../../../commerce-modules/auth/reset-password/page.mdx) to set up a subscriber that sends the customer an email, make sure to use the URL of this page in the notification's data payload.
170
171 </Note>
172
173 For example:
174
175 <CodeTabs group="store-request">
176 <CodeTab label="React" value="react">
177
178 export const resetPasswordHighlights = [
179 ["19", "token", "Receive the token from a query parameter."],
180 ["22", "email", "Receive the email from a query parameter."],
181 ["39", "updateProvider", "Send a request to update the customer's password."],
182 ["41", "password", "Pass the new password in the request body."]
183 ]
184
185 ```tsx highlights={resetPasswordHighlights}
186 "use client" // include with Next.js 13+
187
188 import { useMemo, useState } from "react"
189 import { sdk } from "@/lib/sdk"
190
191 export default function ResetPassword() {
192 const [loading, setLoading] = useState(false)
193 const [password, setPassword] = useState("")
194 // for other than Next.js
195 const searchParams = useMemo(() => {
196 if (typeof window === "undefined") {
197 return
198 }
199
200 return new URLSearchParams(
201 window.location.search
202 )
203 }, [])
204 const token = useMemo(() => {
205 return searchParams?.get("token")
206 }, [searchParams])
207 const email = useMemo(() => {
208 return searchParams?.get("email")
209 }, [searchParams])
210
211 const handleSubmit = async (
212 e: React.FormEvent<HTMLFormElement>
213 ) => {
214 e.preventDefault()
215 if (!token) {
216 return
217 }
218 if (!password) {
219 alert("Password is required")
220 return
221 }
222 setLoading(true)
223
224 sdk.auth.updateProvider("customer", "emailpass", {
225 email,
226 password,
227 }, token)
228 .then(() => {
229 alert("Password reset successfully!")
230 })
231 .catch((error) => {
232 alert(`Couldn't reset password: ${error.message}`)
233 })
234 .finally(() => {
235 setLoading(false)
236 })
237 }
238
239 return (
240 <form onSubmit={handleSubmit}>
241 <label>Password</label>
242 <input
243 placeholder="Password"
244 type="password"
245 value={password}
246 onChange={(e) => setPassword(e.target.value)}
247 />
248 <button type="submit" disabled={loading}>
249 Reset Password
250 </button>
251 </form>
252 )
253 }
254 ```
255
256 </CodeTab>
257 <CodeTab label="JS SDK" value="js-sdk">
258
259 export const resetPasswordFetchHighlights = [
260 ["2", "token", "Receive the token from a query parameter."],
261 ["3", "email", "Receive the email from a query parameter."],
262 ["17", "updateProvider", "Send a request to update the customer's password."],
263 ["19", "password", "Pass the new password in the request body."]
264 ]
265
266 ```ts highlights={resetPasswordFetchHighlights}
267 const queryParams = new URLSearchParams(window.location.search)
268 const token = queryParams.get("token")
269 const email = queryParams.get("email")
270
271 const handleSubmit = async (
272 e: React.FormEvent<HTMLFormElement>
273 ) => {
274 e.preventDefault()
275 if (!token) {
276 return
277 }
278 if (!password) {
279 alert("Password is required")
280 return
281 }
282
283 sdk.auth.updateProvider("customer", "emailpass", {
284 email,
285 password,
286 }, token)
287 .then(() => {
288 alert("Password reset successfully!")
289 })
290 .catch((error) => {
291 alert(`Couldn't reset password: ${error.message}`)
292 })
293 }
294 ```
295
296 </CodeTab>
297 </CodeTabs>
298
299 In this example, you receive the `token` and `email` from the page's query parameters.
300
301 Then, when the form that has the password field is submitted, you send a request to the [Reset Password API route](!api!/store/auth/reset-password), passing it the token, email, and new password.
302
303 Notice that the JS SDK passes the token in the `Authorization: Bearer` header. So, if you're implementing this flow without using the JS SDK, make sure to pass the token accordingly.
304
305 <Note>
306
307 Before [Medusa v2.6](https://github.com/medusajs/medusa/releases/tag/v2.6), you passed the token as a query parameter. Now, you must pass it in the `Authorization: Bearer` header.
308
309 </Note>