Examples of the User Module
In this guide, you’ll find common examples of how you can use the User Module in your application.
Create a User
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
const user = await userModuleService.create({
email: "user@example.com",
first_name: "John",
last_name: "Smith",
})
res.json({ user })
}
import { NextResponse } from "next/server"
import {
initialize as initializeUserModule,
} from "@medusajs/user"
export async function POST(request: Request) {
const userModuleService = await initializeUserModule()
const user = await userModuleService.create({
email: "user@example.com",
first_name: "John",
last_name: "Smith",
})
return NextResponse.json({
user,
})
}
List Users
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
res.json({
users: await userModuleService.list(),
})
}
Update a User
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
const user = await userModuleService.update({
id: "user_123",
last_name: "Smith",
})
res.json({ user })
}
import { NextResponse } from "next/server"
import {
initialize as initializeUserModule,
} from "@medusajs/user"
export async function POST(
request: Request
) {
const userModuleService = await initializeUserModule()
const user = await userModuleService.update({
id: "user_123",
last_name: "Smith",
})
return NextResponse.json({
user,
})
}
Delete a User
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
await userModuleService.delete(["user_123"])
}
Invite User
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
const invite = await userModuleService.createInvites({
email: "user2@example.com",
})
res.json({
invite,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeUserModule,
} from "@medusajs/user"
export async function POST(
request: Request,
{ params }: ContextType
) {
const userModuleService = await initializeUserModule()
const invite = await userModuleService.createInvites({
email: "user2@example.com",
})
return NextResponse.json({
invite,
})
}
Accept Invite
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
const invite =
await userModuleService.validateInviteToken("secret_123")
const user = await userModuleService.create({
email: invite.email,
metadata: invite.metadata,
})
await userModuleService.updateInvites({
id: invite.id,
accepted: true,
})
res.json({
user,
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeUserModule,
} from "@medusajs/user"
export async function POST(
request: Request,
{ params }: ContextType
) {
const userModuleService = await initializeUserModule()
const invite =
await userModuleService.validateInviteToken("secret_123")
const user = await userModuleService.create({
email: invite.email,
metadata: invite.metadata,
})
await userModuleService.updateInvites({
id: invite.id,
accepted: true,
})
return NextResponse.json({
user,
})
}
Refresh Invite
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export async function POST(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const userModuleService: IUserModuleService =
req.scope.resolve(ModuleRegistrationName.USER)
const invites = await userModuleService.refreshInviteTokens([
"invite_123",
])
res.json({
invite: invites[0],
})
}
import { NextResponse } from "next/server"
import {
initialize as initializeUserModule,
} from "@medusajs/user"
export async function POST(
request: Request,
{ params }: ContextType
) {
const userModuleService = await initializeUserModule()
const invites = await userModuleService.refreshInviteTokens([
"invite_123",
])
return NextResponse.json({
invite: invites[0],
})
}
More Examples
The module interface reference provides a reference to all the methods available for use with examples for each.
Was this section helpful?