1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab } from "docs-ui"
6
7 export const metadata = {
8 title: `User Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the User Module and how to use it in your application.
14
15 <Note title="Looking for no-code docs?">
16
17 Refer to the [Medusa Admin User Guide](!user-guide!/settings/users) to learn how to manage users using the dashboard.
18
19 </Note>
20
21 Medusa has user related features available out-of-the-box through the User Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this User Module.
22
23 <Note>
24
25 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
26
27 </Note>
28
29 ## User Features
30
31 - [User Management](./user-creation-flows/page.mdx): Store and manage users in your store.
32 - [Invite Users](./user-creation-flows/page.mdx#invite-users): Invite users to join your store and manage those invites.
33
34 ---
35
36 ## How to Use User Module's Service
37
38 In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
39
40 You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package.
41
42 For example:
43
44 export const highlights = [
45 ["12", "Modules.USER", "Resolve the module in a step."]
46 ]
47
48 ```ts title="src/workflows/create-user.ts" highlights={highlights}
49 import {
50 createWorkflow,
51 WorkflowResponse,
52 createStep,
53 StepResponse,
54 } from "@medusajs/framework/workflows-sdk"
55 import { Modules } from "@medusajs/framework/utils"
56
57 const createUserStep = createStep(
58 "create-user",
59 async ({}, { container }) => {
60 const userModuleService = container.resolve(Modules.USER)
61
62 const user = await userModuleService.createUsers({
63 email: "user@example.com",
64 first_name: "John",
65 last_name: "Smith",
66 })
67
68 return new StepResponse({ user }, user.id)
69 },
70 async (userId, { container }) => {
71 if (!userId) {
72 return
73 }
74 const userModuleService = container.resolve(Modules.USER)
75
76 await userModuleService.deleteUsers([userId])
77 }
78 )
79
80 export const createUserWorkflow = createWorkflow(
81 "create-user",
82 () => {
83 const { user } = createUserStep()
84
85 return new WorkflowResponse({
86 user,
87 })
88 }
89 )
90 ```
91
92 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
93
94 <CodeTabs group="resource-types">
95 <CodeTab label="API Route" value="api-route">
96
97 ```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
98 import type {
99 MedusaRequest,
100 MedusaResponse,
101 } from "@medusajs/framework/http"
102 import { createUserWorkflow } from "../../workflows/create-user"
103
104 export async function GET(
105 req: MedusaRequest,
106 res: MedusaResponse
107 ) {
108 const { result } = await createUserWorkflow(req.scope)
109 .run()
110
111 res.send(result)
112 }
113 ```
114
115 </CodeTab>
116 <CodeTab label="Subscriber" value="subscriber">
117
118 ```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
119 import {
120 type SubscriberConfig,
121 type SubscriberArgs,
122 } from "@medusajs/framework"
123 import { createUserWorkflow } from "../workflows/create-user"
124
125 export default async function handleUserCreated({
126 event: { data },
127 container,
128 }: SubscriberArgs<{ id: string }>) {
129 const { result } = await createUserWorkflow(container)
130 .run()
131
132 console.log(result)
133 }
134
135 export const config: SubscriberConfig = {
136 event: "user.created",
137 }
138 ```
139
140 </CodeTab>
141 <CodeTab label="Scheduled Job" value="scheduled-job">
142
143 ```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]}
144 import { MedusaContainer } from "@medusajs/framework/types"
145 import { createUserWorkflow } from "../workflows/create-user"
146
147 export default async function myCustomJob(
148 container: MedusaContainer
149 ) {
150 const { result } = await createUserWorkflow(container)
151 .run()
152
153 console.log(result)
154 }
155
156 export const config = {
157 name: "run-once-a-day",
158 schedule: `0 0 * * *`,
159 }
160 ```
161
162 </CodeTab>
163 </CodeTabs>
164
165 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
166
167 ---
168
169 ## Configure User Module
170
171 The User Module accepts options for further configurations. Refer to [this documentation](./module-options/page.mdx) for details on the module's options.
172
173 ---
174
175 <CommerceModuleSections />