1 ---
2 sidebar_label: "Reset Password Email"
3 tags:
4 - notification
5 - how to
6 - server
7 ---
8
9 import { Prerequisites } from "docs-ui"
10
11 export const metadata = {
12 title: `Send Reset Password Email Notification`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to handle the `auth.password_reset` event to send a reset password email (or other notification type) to users.
18
19 <Note title="Looking for no-code docs?">
20
21 Refer to this [Medusa Admin User Guide](!user-guide!/reset-password) to learn how to reset your user admin password using the dashboard.
22
23 </Note>
24
25 ## Reset Password Flow Overview
26
27 ![Diagram showcasing the reset password flow detailed below](https://res.cloudinary.com/dza7lstvk/image/upload/v1754050032/Medusa%20Resources/reset-password_qheixj.jpg)
28
29 Users of any actor type (admin, customer, or custom actor type) can request to reset their password. The flow for resetting a password is as follows:
30
31 1. The user requests to reset their password either through the frontend (for example, [Medusa Admin](!user-guide!/reset-password)) or the [Generate Reset Password Token API route](../authentication-route/page.mdx#generate-reset-password-token-route).
32 2. The Medusa application generates a secure, single-use password reset token and emits the `auth.password_reset` event.
33 - The token is cryptographically secure and expires automatically (default: 15 minutes).
34 - Each new password reset request invalidates any previous tokens for the same user.
35 - At this point, you can handle the event to send a notification to the user with instructions on how to reset their password.
36 3. The user receives the notification and clicks on the link to reset their password.
37 - The user can reset their password either through the frontend (for example, [Medusa Admin](!user-guide!/reset-password)) or the [Reset Password API route](../authentication-route/page.mdx#reset-password-route).
38 - The token is automatically consumed and deleted upon successful password reset, preventing reuse.
39
40 In this guide, you'll implement a subscriber that handles the `auth.password_reset` event to send an email notification to the user with instructions on how to reset their password.
41
42 After adding the subscriber, you will have a complete reset password flow you can utilize using the Medusa Admin, storefront, or API routes.
43
44 ---
45
46 ## Prerequisites: Notification Module Provider
47
48 To send an email or notification to the user, you must have a Notification Module Provider set up.
49
50 Medusa provides providers like [SendGrid](../../../infrastructure-modules/notification/sendgrid/page.mdx) and [Resend](../../../integrations/guides/resend/page.mdx), and you can also [create your own custom provider](/references/notification-provider-module).
51
52 Refer to the [Notification Module](../../../infrastructure-modules/notification/page.mdx#what-is-a-notification-module-provider) documentation for a list of available providers and how to set them up.
53
54 ### Testing with the Local Notification Module Provider
55
56 For testing purposes, you can use the [Local Notification Module Provider](../../../infrastructure-modules/notification/local/page.mdx) by adding this to your `medusa-config.ts`:
57
58 ```ts title="medusa-config.ts"
59 module.exports = defineConfig({
60 // ...
61 modules: [
62 {
63 resolve: "@medusajs/medusa/notification",
64 options: {
65 providers: [
66 // ...
67 {
68 resolve: "@medusajs/medusa/notification-local",
69 id: "local",
70 options: {
71 channels: ["email"],
72 },
73 },
74 ],
75 },
76 },
77 ],
78 })
79 ```
80
81 The Local provider logs email details to your terminal instead of sending actual emails, which is useful for development and testing.
82
83 ---
84
85 ## Create the Reset Password Subscriber
86
87 To create a [subscriber](!docs!/learn/fundamentals/events-and-subscribers) that handles the `auth.password_reset` event, create the file `src/subscribers/password-reset.ts` with the following content:
88
89 export const highlights=[
90 ["11", "data", "The data payload of the event."],
91 ["12", "entity_id", "The user's identifier, which is the email when using the `emailpass` provider."],
92 ["13", "token", "The password reset token."],
93 ["14", "actor_type", "The user's actor type."],
94 ["25", "urlPrefix", "Set the page's URL based on the user's actor type."],
95 ["36", "createNotifications", "Send a notification to the user."],
96 ["38", `"email"`, "The channel to send the notification through."],
97 ["40", "template", "The template defined in the third-party provider."],
98 ["41", "data", "The data to pass to the template in the third-party provider."],
99 ["43", "reset_url", "The frontend URL to redirect the user to reset their password."]
100 ]
101
102 ```ts title="src/subscribers/handle-reset.ts" highlights={highlights} collapsibleLines="1-9" expandMoreLabel="Show Imports"
103 import {
104 SubscriberArgs,
105 type SubscriberConfig,
106 } from "@medusajs/medusa"
107 import {
108 Modules,
109 ContainerRegistrationKeys,
110 } from "@medusajs/framework/utils"
111
112 export default async function resetPasswordTokenHandler({
113 event: { data: {
114 entity_id: email,
115 token,
116 actor_type,
117 } },
118 container,
119 }: SubscriberArgs<{ entity_id: string, token: string, actor_type: string }>) {
120 const notificationModuleService = container.resolve(
121 Modules.NOTIFICATION
122 )
123 const config = container.resolve(
124 ContainerRegistrationKeys.CONFIG_MODULE
125 )
126
127 let urlPrefix = ""
128
129 if (actor_type === "customer") {
130 urlPrefix = config.admin.storefrontUrl || "https://storefront.com"
131 } else {
132 const backendUrl = config.admin.backendUrl !== "/" ? config.admin.backendUrl :
133 "http://localhost:9000"
134 const adminPath = config.admin.path
135 urlPrefix = `${backendUrl}${adminPath}`
136 }
137
138 await notificationModuleService.createNotifications({
139 to: email,
140 channel: "email",
141 // TODO replace with template ID in notification provider
142 template: "password-reset",
143 data: {
144 // a URL to a frontend application
145 reset_url: `${urlPrefix}/reset-password?token=${token}&email=${email}`,
146 },
147 })
148 }
149
150 export const config: SubscriberConfig = {
151 event: "auth.password_reset",
152 }
153 ```
154
155 The subscriber receives the following data through the event payload:
156
157 - `entity_id`: The identifier of the user. When using the `emailpass` provider, it's the user's email.
158 - `token`: The token to reset the user's password.
159 - `actor_type`: The user's actor type. For example, if the user is a customer, the `actor_type` is `customer`. If it's an admin user, the `actor_type` is `user`.
160
161 <Note>
162
163 This event's payload previously had an `actorType` field. It was renamed to `actor_type` after [Medusa v2.0.7](https://github.com/medusajs/medusa/releases/tag/v2.0.7).
164
165 </Note>
166
167 ### Reset Password URL
168
169 Based on the user's actor type, you set the URL prefix to redirect the user to the appropriate frontend page to reset their password:
170
171 - If the user is a customer, you set the URL prefix to the storefront URL.
172 - If the user is an admin, you set the URL prefix to the backend URL, which is constructed from the `config.admin.backendUrl` and `config.admin.path` values.
173
174 Note that the Medusa Admin has a reset password form at `/reset-password?token={token}&email={email}`.
175
176 ### Notification Configurations
177
178 For the notification, you can configure the following fields:
179
180 - `to`: The identifier to send the notification to, which in this case is the email.
181 - `channel`: The channel to send the notification through, which in this case is email.
182 - `template`: The template ID of the email to send. This ID depends on the Notification Module provider you use. For example, if you use SendGrid, this would be the ID of the SendGrid template.
183 - Refer to the [Example Notification Templates](#example-notification-templates) section below for examples of notification templates to use.
184 - `data`: The data payload to pass to the template. You can pass additional fields, if necessary.
185
186 ### Test It Out
187
188 After you set up the Notification Module Provider, create a template in the provider, and create the subscriber, you can test the reset password flow.
189
190 Start the Medusa application with the following command:
191
192 ```bash npm2yarn
193 npm run dev
194 ```
195
196 Then, open the Medusa Admin (locally at `http://localhost:9000/app`) and click on "Reset" in the login form. Then, enter the email of the user you want to reset the password for.
197
198 Once you reset the password, you should see that the `auth.password_reset` event is emitted in the server's logs:
199
200 ```bash
201 info: Processing auth.password_reset which has 1 subscribers
202 ```
203
204 If you're using an email Notification Module Provider, check the user's email inbox for the reset password email with the link to reset their password.
205
206 If you're using the Local provider, check your terminal for the logged email details.
207
208 ---
209
210 ## Next Steps: Implementing Frontend
211
212 In your frontend, you must have a page that accepts `token` and `email` query parameters.
213
214 The page shows the user password fields to enter their new password, then submits the new password, token, and email to the [Reset Password Route](../authentication-route/page.mdx#reset-password-route).
215
216 The Medusa Admin already has a reset password page at `/reset-password?token={token}&email={email}`. So, you only need to implement this page in your storefront or custom admin dashboard.
217
218 ### Examples
219
220 - [Storefront Guide: Reset Customer Password](../../../storefront-development/customers/reset-password/page.mdx)
221
222 ---
223
224 ## Example Notification Templates
225
226 The following section provides example notification templates for some Notification Module Providers.
227
228 ### SendGrid
229
230 <Note>
231
232 Refer to the [SendGrid Notification Module Provider](../../../infrastructure-modules/notification/sendgrid/page.mdx) documentation for more details on how to set up SendGrid.
233
234 </Note>
235
236 The following HTML template can be used with SendGrid to send a reset password email:
237
238 ```html
239 <!DOCTYPE html>
240 <html>
241 <head>
242 <meta charset="utf-8">
243 <meta name="viewport" content="width=device-width, initial-scale=1.0">
244 <title>Reset Your Password</title>
245 <style>
246 body {
247 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
248 background-color: #ffffff;
249 margin: 0;
250 padding: 20px;
251 }
252 .container {
253 max-width: 465px;
254 margin: 40px auto;
255 border: 1px solid #eaeaea;
256 border-radius: 5px;
257 padding: 20px;
258 }
259 .header {
260 text-align: center;
261 margin: 30px 0;
262 }
263 .title {
264 color: #000000;
265 font-size: 24px;
266 font-weight: normal;
267 margin: 0;
268 }
269 .content {
270 margin: 32px 0;
271 }
272 .text {
273 color: #000000;
274 font-size: 14px;
275 line-height: 24px;
276 margin: 0 0 16px 0;
277 }
278 .button-container {
279 text-align: center;
280 margin: 32px 0;
281 }
282 .reset-button {
283 background-color: #000000;
284 border-radius: 3px;
285 color: #ffffff;
286 font-size: 12px;
287 font-weight: 600;
288 text-decoration: none;
289 text-align: center;
290 padding: 12px 20px;
291 display: inline-block;
292 }
293 .reset-button:hover {
294 background-color: #333333;
295 }
296 .url-section {
297 margin: 32px 0;
298 }
299 .url-link {
300 color: #2563eb;
301 text-decoration: none;
302 font-size: 14px;
303 line-height: 24px;
304 word-break: break-all;
305 }
306 .disclaimer {
307 margin: 32px 0;
308 }
309 .disclaimer-text {
310 color: #666666;
311 font-size: 12px;
312 line-height: 24px;
313 margin: 0 0 8px 0;
314 }
315 .security-footer {
316 margin-top: 32px;
317 padding-top: 20px;
318 border-top: 1px solid #eaeaea;
319 }
320 .security-text {
321 color: #666666;
322 font-size: 12px;
323 line-height: 24px;
324 margin: 0;
325 }
326 </style>
327 </head>
328 <body>
329 <div class="container">
330 <div class="header">
331 <h1 class="title">Reset Your Password</h1>
332 </div>
333
334 <div class="content">
335 <p class="text">
336 Hello{{#if email}} {{email}}{{/if}},
337 </p>
338 <p class="text">
339 We received a request to reset your password. Click the button below to create a new password for your account.
340 </p>
341 </div>
342
343 <div class="button-container">
344 <a href="{{reset_url}}" class="reset-button">
345 Reset Password
346 </a>
347 </div>
348
349 <div class="url-section">
350 <p class="text">
351 Or copy and paste this URL into your browser:
352 </p>
353 <a href="{{reset_url}}" class="url-link">
354 {{reset_url}}
355 </a>
356 </div>
357
358 <div class="disclaimer">
359 <p class="disclaimer-text">
360 This password reset link will expire soon for security reasons.
361 </p>
362 <p class="disclaimer-text">
363 If you didn't request a password reset, you can safely ignore this email. Your password will remain unchanged.
364 </p>
365 </div>
366
367 <div class="security-footer">
368 <p class="security-text">
369 For security reasons, never share this reset link with anyone. If you're having trouble with the button above, copy and paste the URL into your web browser.
370 </p>
371 </div>
372 </div>
373 </body>
374 </html>
375 ```
376
377 Make sure to pass the `reset_url` variable to the template, which contains the URL to reset the password.
378
379 You can also customize the template further to show other information.
380
381 ### Resend
382
383 If you've integrated Resend as explained in the [Resend Integration Guide](../../../integrations/guides/resend/page.mdx), you can add a new template for password reset emails at `src/modules/resend/emails/password-reset.tsx`:
384
385 ```tsx title="src/modules/resend/emails/password-reset.tsx"
386 import {
387 Text,
388 Container,
389 Heading,
390 Html,
391 Section,
392 Tailwind,
393 Head,
394 Preview,
395 Body,
396 Link,
397 Button,
398 } from "@react-email/components"
399
400 type PasswordResetEmailProps = {
401 reset_url: string
402 email?: string
403 }
404
405 function PasswordResetEmailComponent({ reset_url, email }: PasswordResetEmailProps) {
406 return (
407 <Html>
408 <Head />
409 <Preview>Reset your password</Preview>
410 <Tailwind>
411 <Body className="bg-white my-auto mx-auto font-sans px-2">
412 <Container className="border border-solid border-[#eaeaea] rounded my-[40px] mx-auto p-[20px] max-w-[465px]">
413 <Section className="mt-[32px]">
414 <Heading className="text-black text-[24px] font-normal text-center p-0 my-[30px] mx-0">
415 Reset Your Password
416 </Heading>
417 </Section>
418
419 <Section className="my-[32px]">
420 <Text className="text-black text-[14px] leading-[24px]">
421 Hello{email ? ` ${email}` : ""},
422 </Text>
423 <Text className="text-black text-[14px] leading-[24px]">
424 We received a request to reset your password. Click the button below to create a new password for your account.
425 </Text>
426 </Section>
427
428 <Section className="text-center mt-[32px] mb-[32px]">
429 <Button
430 className="bg-[#000000] rounded text-white text-[12px] font-semibold no-underline text-center px-5 py-3"
431 href={reset_url}
432 >
433 Reset Password
434 </Button>
435 </Section>
436
437 <Section className="my-[32px]">
438 <Text className="text-black text-[14px] leading-[24px]">
439 Or copy and paste this URL into your browser:
440 </Text>
441 <Link
442 href={reset_url}
443 className="text-blue-600 no-underline text-[14px] leading-[24px] break-all"
444 >
445 {reset_url}
446 </Link>
447 </Section>
448
449 <Section className="my-[32px]">
450 <Text className="text-[#666666] text-[12px] leading-[24px]">
451 This password reset link will expire soon for security reasons.
452 </Text>
453 <Text className="text-[#666666] text-[12px] leading-[24px] mt-2">
454 If you didn't request a password reset, you can safely ignore this email. Your password will remain unchanged.
455 </Text>
456 </Section>
457
458 <Section className="mt-[32px] pt-[20px] border-t border-solid border-[#eaeaea]">
459 <Text className="text-[#666666] text-[12px] leading-[24px]">
460 For security reasons, never share this reset link with anyone. If you're having trouble with the button above, copy and paste the URL into your web browser.
461 </Text>
462 </Section>
463 </Container>
464 </Body>
465 </Tailwind>
466 </Html>
467 )
468 }
469
470 export const passwordResetEmail = (props: PasswordResetEmailProps) => (
471 <PasswordResetEmailComponent {...props} />
472 )
473
474 // Mock data for preview/development
475 const mockPasswordReset: PasswordResetEmailProps = {
476 reset_url: "https://your-app.com/reset-password?token=sample-reset-token-123",
477 email: "user@example.com",
478 }
479
480 export default () => <PasswordResetEmailComponent {...mockPasswordReset} />
481 ```
482
483 Feel free to customize the email template further to match your branding and style, or to add additional information.
484
485 Then, in the Resend Module's service at `src/modules/resend/service.ts`, add the new template to the `templates` object and `Templates` type:
486
487 ```ts title="src/modules/resend/service.ts"
488 // other imports...
489 import { passwordResetEmail } from "./emails/password-reset"
490
491 enum Templates {
492 // ...
493 PASSWORD_RESET = "password-reset",
494 }
495
496 const templates: {[key in Templates]?: (props: unknown) => React.ReactNode} = {
497 // ...
498 [Templates.PASSWORD_RESET]: passwordResetEmail,
499 }
500 ```
501
502 Finally, find the `getTemplateSubject` function in the `ResendNotificationProviderService` and add a case for the `USER_INVITED` template:
503
504 ```ts title="src/modules/resend/service.ts"
505 class ResendNotificationProviderService extends AbstractNotificationProviderService {
506 // ...
507
508 private getTemplateSubject(template: Templates) {
509 // ...
510 switch (template) {
511 // ...
512 case Templates.PASSWORD_RESET:
513 return "Reset Your Password"
514 }
515 }
516 }
517 ```