Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab, ChildDocs } from "docs-ui"
6
7 export const metadata = {
8 title: `Auth Module`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you will find resources to learn more about the Auth Module and how to use it in your application.
14
15 Medusa has auth related features available out-of-the-box through the Auth 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 Auth Module.
16
17 <Note>
18
19 Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation).
20
21 </Note>
22
23 ## Auth Features
24
25 - [Basic User Authentication](./authentication-route/page.mdx#1-basic-authentication-flow): Authenticate users using their email and password credentials.
26 - [Third-Party and Social Authentication](./authentication-route/page.mdx#2-third-party-service-authenticate-flow): Authenticate users using third-party services and social platforms, such as [Google](./auth-providers/google/page.mdx) and [GitHub](./auth-providers/github/page.mdx).
27 - [Authenticate Custom Actor Types](./create-actor-type/page.mdx): Create custom user or actor types, such as managers, authenticate them in your application, and guard routes based on the custom user types.
28 - [Email Verification](./email-verification/page.mdx): Require email verification for new registrations and authentications.
29 - [Multi-Factor Authentication (MFA)](./mfa/page.mdx): Add an extra layer of security with time-based one-time passwords (TOTP), backup recovery codes, and configurable disable policies.
30 - [Custom Authentication Providers](/references/auth/provider): Integrate third-party services with custom authentication providers.
31
32 ---
33
34 ## How to Use the Auth Module
35
36 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.
37
38 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.
39
40 For example:
41
42 export const highlights = [
43 ["18", "Modules.AUTH", "Resolve the module in a step."]
44 ]
45
46 ```ts title="src/workflows/authenticate-user.ts" highlights={highlights}
47 import {
48 createWorkflow,
49 WorkflowResponse,
50 createStep,
51 StepResponse,
52 } from "@medusajs/framework/workflows-sdk"
53 import { Modules, MedusaError } from "@medusajs/framework/utils"
54 import { MedusaRequest } from "@medusajs/framework/http"
55 import { AuthenticationInput } from "@medusajs/framework/types"
56
57 type Input = {
58 req: MedusaRequest
59 }
60
61 const authenticateUserStep = createStep(
62 "authenticate-user",
63 async ({ req }: Input, { container }) => {
64 const authModuleService = container.resolve(Modules.AUTH)
65
66 const { success, authIdentity, error } = await authModuleService
67 .authenticate(
68 "emailpass",
69 {
70 url: req.url,
71 headers: req.headers,
72 query: req.query,
73 body: req.body,
74 authScope: "admin", // or custom actor type
75 protocol: req.protocol,
76 } as AuthenticationInput
77 )
78
79 if (!success) {
80 // incorrect authentication details
81 throw new MedusaError(
82 MedusaError.Types.UNAUTHORIZED,
83 error || "Incorrect authentication details"
84 )
85 }
86
87 return new StepResponse({ authIdentity }, authIdentity?.id)
88 },
89 async (authIdentityId, { container }) => {
90 if (!authIdentityId) {
91 return
92 }
93
94 const authModuleService = container.resolve(Modules.AUTH)
95
96 await authModuleService.deleteAuthIdentities([authIdentityId])
97 }
98 )
99
100 export const authenticateUserWorkflow = createWorkflow(
101 "authenticate-user",
102 (input: Input) => {
103 const { authIdentity } = authenticateUserStep(input)
104
105 return new WorkflowResponse({
106 authIdentity,
107 })
108 }
109 )
110 ```
111
112 You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers:
113
114 ```ts title="API Route" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports"
115 import type {
116 MedusaRequest,
117 MedusaResponse,
118 } from "@medusajs/framework/http"
119 import { authenticateUserWorkflow } from "../../workflows/authenticate-user"
120
121 export async function GET(
122 req: MedusaRequest,
123 res: MedusaResponse
124 ) {
125 const { result } = await authenticateUserWorkflow(req.scope)
126 .run({
127 req,
128 })
129
130 res.send(result)
131 }
132 ```
133
134 Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows).
135
136 ---
137
138 ## Configure Auth Module
139
140 The Auth Module accepts options for further configurations. Refer to [this documentation](./module-options/page.mdx) for details on the module's options.
141
142 ---
143
144 ## Providers
145
146 Medusa provides the following authentication providers out-of-the-box. You can use them to authenticate admin users, customers, or custom actor types.
147
148 <ChildDocs showItems={["Providers"]} hideTitle />
149
150 ---
151
152 <CommerceModuleSections />