import { CodeTabs, CodeTab } from "docs-ui"

export const metadata = {
  title: `Examples of the User Module`,
}

# {metadata.title}

In this guide, you’ll find common examples of how you can use the User Module in your application.

## Create a User

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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 })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## List Users

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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(),
    })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  import { NextResponse } from "next/server"

  import {
    initialize as initializeUserModule,
  } from "@medusajs/user"

  export async function GET(request: Request) {
    const userModuleService = await initializeUserModule()

    return NextResponse.json({
      users: await userModuleService.list(),
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## Update a User

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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 })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## Delete a User

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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"])
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  import { NextResponse } from "next/server"

  import {
    initialize as initializeUserModule,
  } from "@medusajs/user"

  export async function DELETE(
    request: Request
  ) {
    const userModuleService = await initializeUserModule()

    await userModuleService.delete(["user_123"])
  }
  ```

  </CodeTab>
</CodeTabs>

---

## Invite User

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## Accept Invite

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  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,
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## Refresh Invite

<CodeTabs groupId="app-type">
  <CodeTab label="Medusa API Router" value="medusa">

  ```ts
  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],
    })
  }
  ```

  </CodeTab>
  <CodeTab label="Next.js App Router" value="nextjs">

  ```ts
  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],
    })
  }
  ```

  </CodeTab>
</CodeTabs>

---

## More Examples

The [module interface reference](/references/user) provides a reference to all the methods available for use with examples for each.
