docs: improved commerce modules [5/5] (#9592)
- Improve remaining commerce modules - Other: add a note about using methods of the modules' main services.
This commit is contained in:
@@ -8,21 +8,26 @@ export const metadata = {
|
||||
|
||||
In this guide, you’ll find common examples of how you can use the User Module in your application.
|
||||
|
||||
<Note>
|
||||
|
||||
You should only use the User Module's main service when implementing complex customizations. For common cases, check out [available workflows instead](../../../medusa-workflows-reference/page.mdx).
|
||||
|
||||
</Note>
|
||||
|
||||
## Create a User
|
||||
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -69,16 +74,15 @@ export async function POST(request: Request) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -115,16 +119,15 @@ export async function GET(request: Request) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -169,16 +172,15 @@ export async function POST(request: Request) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function DELETE(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -211,16 +213,15 @@ export async function DELETE(request: Request) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -265,16 +266,15 @@ export async function POST(request: Request, { params }: ContextType) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -335,16 +335,15 @@ export async function POST(request: Request, { params }: ContextType) {
|
||||
<CodeTabs groupId="app-type">
|
||||
<CodeTab label="Medusa API Router" value="medusa">
|
||||
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function POST(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ export const metadata = {
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
The User Module is the `@medusajs/medusa/user` NPM package that provides user-related features in your Medusa and Node.js applications.
|
||||
The User Module provides user-related features in your Medusa and Node.js applications.
|
||||
|
||||
## How to Use User Module's Service
|
||||
|
||||
@@ -15,18 +15,33 @@ You can use the User Module's main service by resolving from the Medusa containe
|
||||
For example:
|
||||
|
||||
<CodeTabs groupId="resource-type">
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const userModuleService = container.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="API Route" value="api-route">
|
||||
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts title="src/api/store/custom/route.ts"
|
||||
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export async function GET(
|
||||
req: MedusaRequest,
|
||||
res: MedusaResponse
|
||||
): Promise<void> {
|
||||
const userModuleService: IUserModuleService = req.scope.resolve(
|
||||
const userModuleService = req.scope.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
@@ -39,35 +54,17 @@ export async function GET(
|
||||
</CodeTab>
|
||||
<CodeTab label="Subscriber" value="subscribers">
|
||||
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/framework"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
```ts title="src/subscribers/custom-handler.ts"
|
||||
import { SubscriberArgs } from "@medusajs/framework"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
export default async function subscriberHandler({ container }: SubscriberArgs) {
|
||||
const userModuleService: IUserModuleService = container.resolve(
|
||||
const userModuleService = container.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
}
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
<CodeTab label="Workflow Step" value="workflow-step">
|
||||
|
||||
```ts title="src/workflows/hello-world/step1.ts"
|
||||
import { createStep } from "@medusajs/framework/workflows-sdk"
|
||||
import { IUserModuleService } from "@medusajs/framework/types"
|
||||
import { Modules } from "@medusajs/framework/utils"
|
||||
|
||||
const step1 = createStep("step-1", async (_, { container }) => {
|
||||
const userModuleService: IUserModuleService = container.resolve(
|
||||
Modules.USER
|
||||
)
|
||||
|
||||
const users = await userModuleService.listUsers()
|
||||
})
|
||||
```
|
||||
|
||||
</CodeTab>
|
||||
|
||||
@@ -4,21 +4,37 @@ export const metadata = {
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
This document provides flows to create a user.
|
||||
In this document, learn the different ways to create a user using the User Module.
|
||||
|
||||
## Straightforward User Creation
|
||||
|
||||
To create a user, use the [create method of the User Module’s main service](/references/user/create):
|
||||
|
||||
```ts
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
})
|
||||
```
|
||||
|
||||
You can pair this with the Auth Module to allow the user to authenticate, as explained in a [later section](#create-identity-with-the-auth-module).
|
||||
|
||||
---
|
||||
|
||||
## Invite Users
|
||||
|
||||
Another possible flow to create a user is by sending them an invite. Then, once they accept it, you create a new user for them:
|
||||
To create a user, you can create an invite for them using the [createInvites method](/references/user/createInvites) of the User Module's main service:
|
||||
|
||||
```ts
|
||||
// create invite
|
||||
const invite = await userModuleService.createInvites({
|
||||
email: "user@example.com",
|
||||
})
|
||||
```
|
||||
|
||||
// later, accept invite and create user
|
||||
Later, you can accept the invite and create a new user for them:
|
||||
|
||||
```ts
|
||||
const invite =
|
||||
await userModuleService.validateInviteToken("secret123")
|
||||
await userModuleService.validateInviteToken("secret_123")
|
||||
|
||||
await userModuleService.updateInvites({
|
||||
id: invite.id,
|
||||
@@ -32,7 +48,7 @@ const user = await userModuleService.createUsers({
|
||||
|
||||
### Invite Expiry
|
||||
|
||||
An invite has an expiry date. You can renew the expiry date and refresh the token using the `refreshInviteTokens` method:
|
||||
An invite has an expiry date. You can renew the expiry date and refresh the token using the [refreshInviteTokens method](/references/user/refreshInviteTokens):
|
||||
|
||||
```ts
|
||||
await userModuleService.refreshInviteTokens(["invite_123"])
|
||||
@@ -40,17 +56,7 @@ await userModuleService.refreshInviteTokens(["invite_123"])
|
||||
|
||||
---
|
||||
|
||||
## Straightforward Creation
|
||||
|
||||
Finally, you can create a user using the [create method of the User Module’s main service](/references/user/create):
|
||||
|
||||
```ts
|
||||
const user = await userModuleService.createUsers({
|
||||
email: "user@example.com",
|
||||
})
|
||||
```
|
||||
|
||||
### With the Auth Module
|
||||
## Create Identity with the Auth Module
|
||||
|
||||
By combining the User and Auth Modules, you can use the Auth Module for authenticating users, and the User Module to manage those users.
|
||||
|
||||
@@ -62,7 +68,6 @@ const { success, authIdentity } =
|
||||
// ...
|
||||
})
|
||||
|
||||
// assuming authIdentity is defined
|
||||
const [, count] = await userModuleService.listAndCountUsers({
|
||||
email: authIdentity.entity_id,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user