* initialized next.js project * finished markdown sections * added operation schema component * change page metadata * eslint fixes * fixes related to deployment * added response schema * resolve max stack issue * support for different property types * added support for property types * added loading for components * added more loading * type fixes * added oneOf type * removed console * fix replace with push * refactored everything * use static content for description * fixes and improvements * added code examples section * fix path name * optimizations * fixed tag navigation * add support for admin and store references * general enhancements * optimizations and fixes * fixes and enhancements * added search bar * loading enhancements * added loading * added code blocks * added margin top * add empty response text * fixed oneOf parameters * added path and query parameters * general fixes * added base path env variable * small fix for arrays * enhancements * design enhancements * general enhancements * fix isRequired * added enum values * enhancements * general fixes * general fixes * changed oas generation script * additions to the introduction section * added copy button for code + other enhancements * fix response code block * fix metadata * formatted store introduction * move sidebar logic to Tags component * added test env variables * fix code block bug * added loading animation * added expand param + loading * enhance operation loading * made responsive + improvements * added loading provider * fixed loading * adjustments for small devices * added sidebar label for endpoints * added feedback component * fixed analytics * general fixes * listen to scroll for other headings * added sample env file * update api ref files + support new fields * fix for external docs link * added new sections * fix last item in sidebar not showing * move docs content to www/docs * change redirect url * revert change * resolve build errors * configure rewrites * changed to environment variable url * revert changing environment variable name * add environment variable for API path * fix links * fix tailwind settings * remove vercel file * reconfigured api route * move api page under api * fix page metadata * fix external link in navigation bar * update api spec * updated api specs * fixed google lint error * add max-height on request samples * add padding before loading * fix for one of name * fix undefined types * general fixes * remove response schema example * redesigned navigation bar * redesigned sidebar * fixed up paddings * added feedback component + report issue * fixed up typography, padding, and general styling * redesigned code blocks * optimization * added error timeout * fixes * added indexing with algolia + fixes * fix errors with algolia script * redesign operation sections * fix heading scroll * design fixes * fix padding * fix padding + scroll issues * fix scroll issues * improve scroll performance * fixes for safari * optimization and fixes * fixes to docs + details animation * padding fixes for code block * added tab animation * fixed incorrect link * added selection styling * fix lint errors * redesigned details component * added detailed feedback form * api reference fixes * fix tabs * upgrade + fixes * updated documentation links * optimizations to sidebar items * fix spacing in sidebar item * optimizations and fixes * fix endpoint path styling * remove margin * final fixes * change margin on small devices * generated OAS * fixes for mobile * added feedback modal * optimize dark mode button * fixed color mode useeffect * minimize dom size * use new style system * radius and spacing design system * design fixes * fix eslint errors * added meta files * change cron schedule * fix docusaurus configurations * added operating system to feedback data * change content directory name * fixes to contribution guidelines * revert renaming content * added api-reference to documentation workflow * fixes for search * added dark mode + fixes * oas fixes * handle bugs * added code examples for clients * changed tooltip text * change authentication to card * change page title based on selected section * redesigned mobile navbar * fix icon colors * fix key colors * fix medusa-js installation command * change external regex in algolia * change changeset * fix padding on mobile * fix hydration error * update depedencies
366 lines
8.1 KiB
Plaintext
366 lines
8.1 KiB
Plaintext
---
|
||
description: 'Learn how to manage users using the admin APIs. This includes listing, creating, updating, and deleting users.'
|
||
addHowToData: true
|
||
---
|
||
|
||
import Tabs from '@theme/Tabs';
|
||
import TabItem from '@theme/TabItem';
|
||
|
||
# How to Manage Users
|
||
|
||
In this document, you’ll learn how to manage users using the admin APIs.
|
||
|
||
## Overview
|
||
|
||
You can use the user admin API to manage users and teams in the store.
|
||
|
||
### Scenario
|
||
|
||
You want to add or use the following admin functionalities:
|
||
|
||
- List users
|
||
- Create a user
|
||
- Update a user
|
||
- Delete a user
|
||
|
||
---
|
||
|
||
## Prerequisites
|
||
|
||
### Medusa Components
|
||
|
||
It is assumed that you already have a Medusa backend installed and set up. If not, you can follow the [quickstart guide](../../../development/backend/install.mdx) to get started.
|
||
|
||
### JS Client
|
||
|
||
This guide includes code snippets to send requests to your Medusa backend using Medusa’s JS Client, among other methods.
|
||
|
||
If you follow the JS Client code blocks, it’s assumed you already have [Medusa’s JS Client](../../../js-client/overview.md) installed and have [created an instance of the client](../../../js-client/overview.md#configuration).
|
||
|
||
### Medusa React
|
||
|
||
This guide also includes code snippets to send requests to your Medusa backend using Medusa React, among other methods.
|
||
|
||
If you follow the Medusa React code blocks, it's assumed you already have [Medusa React installed](../../../medusa-react/overview.mdx) and have [used MedusaProvider higher in your component tree](../../../medusa-react/overview.mdx#usage).
|
||
|
||
### Authenticated Admin User
|
||
|
||
You must be an authenticated admin user before following along with the steps in the tutorial.
|
||
|
||
You can learn more about [authenticating as an admin user in the API reference](https://docs.medusajs.com/api/admin#authentication).
|
||
|
||
---
|
||
|
||
## List Users
|
||
|
||
You can retrieve users in a store by sending a request to the [List Users endpoint](https://docs.medusajs.com/api/admin#users_getusers):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.users.list()
|
||
.then(({ users }) => {
|
||
console.log(users.length)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminUsers } from "medusa-react"
|
||
|
||
const Users = () => {
|
||
const { users, isLoading } = useAdminUsers()
|
||
|
||
return (
|
||
<div>
|
||
{isLoading && <span>Loading...</span>}
|
||
{users && !users.length && <span>No Users</span>}
|
||
{users && users.length > 0 && (
|
||
<ul>
|
||
{users.map((user) => (
|
||
<li key={user.id}>{user.email}</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
export default Users
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/users`, {
|
||
credentials: "include",
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ users }) => {
|
||
console.log(users.length)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X GET '<BACKEND_URL>/admin/users' \
|
||
-H 'Authorization: Bearer <API_TOKEN>'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint does not require any parameters.
|
||
|
||
The request returns an array of user objects.
|
||
|
||
---
|
||
|
||
## Create a User
|
||
|
||
You can create a user by sending a request to the [Create User endpoint](https://docs.medusajs.com/api/admin#users_postusers):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.users.create({
|
||
email: "user@example.com",
|
||
password: "supersecret",
|
||
})
|
||
.then(({ user }) => {
|
||
console.log(user.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminCreateUser } from "medusa-react"
|
||
|
||
const CreateUser = () => {
|
||
const createUser = useAdminCreateUser()
|
||
// ...
|
||
|
||
const handleCreateUser = () => {
|
||
createUser.mutate({
|
||
email: "user@example.com",
|
||
password: "supersecret",
|
||
})
|
||
}
|
||
|
||
// ...
|
||
}
|
||
|
||
export default CreateUser
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/users`, {
|
||
credentials: "include",
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
email: "user@example.com",
|
||
password: "supersecret",
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ user }) => {
|
||
console.log(user.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X POST '<BACKEND_URL>/admin/users' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"email": "user@example.com",
|
||
"password": "supersecret"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the following request body parameters:
|
||
|
||
- `email`: a string indicating the email of the user.
|
||
- `password`: a string indicating the password of the user.
|
||
|
||
The endpoint accepts other optional body parameters, such as first name or last name. Check the [API reference](https://docs.medusajs.com/api/admin#users_postusers) for details on optional body parameters.
|
||
|
||
The request returns the created user as an object.
|
||
|
||
---
|
||
|
||
## Update User
|
||
|
||
You can update a user’s details by sending a request to the [Update User endpoint](https://docs.medusajs.com/api/admin#users_postusersuser):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.users.update(userId, {
|
||
first_name: "Marcellus",
|
||
})
|
||
.then(({ user }) => {
|
||
console.log(user.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminUpdateUser } from "medusa-react"
|
||
|
||
const UpdateUser = () => {
|
||
const updateUser = useAdminUpdateUser(userId)
|
||
// ...
|
||
|
||
const handleUpdateUser = () => {
|
||
updateUser.mutate({
|
||
first_name: "Marcellus",
|
||
})
|
||
}
|
||
|
||
// ...
|
||
}
|
||
|
||
export default UpdateUser
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/users/${userId}`, {
|
||
credentials: "include",
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify({
|
||
first_name: "Marcellus",
|
||
}),
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ user }) => {
|
||
console.log(user.id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X POST '<BACKEND_URL>/admin/users/<USER_ID>' \
|
||
-H 'Authorization: Bearer <API_TOKEN>' \
|
||
-H 'Content-Type: application/json' \
|
||
--data-raw '{
|
||
"first_name": "Marcellus"
|
||
}'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the ID of the user as a path parameter.
|
||
|
||
In the request body, you can pass any of the user’s fields that you want to update as parameters. In the example above, you update the user’s `first_name`. Check the [API reference](https://docs.medusajs.com/api/admin#users_postusersuser) for all the optional parameters.
|
||
|
||
The request returns the updated user as an object.
|
||
|
||
---
|
||
|
||
## Delete a User
|
||
|
||
You can delete a user by sending a request to the [Delete User endpoint](https://docs.medusajs.com/api/admin#users_deleteusersuser):
|
||
|
||
<Tabs groupId="request-type" isCodeTabs={true}>
|
||
<TabItem value="client" label="Medusa JS Client" default>
|
||
|
||
```ts
|
||
medusa.admin.users.delete(userId)
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="medusa-react" label="Medusa React">
|
||
|
||
```tsx
|
||
import { useAdminDeleteUser } from "medusa-react"
|
||
|
||
const DeleteUser = () => {
|
||
const deleteUser = useAdminDeleteUser(userId)
|
||
// ...
|
||
|
||
const handleDeleteUser = () => {
|
||
deleteUser.mutate()
|
||
}
|
||
|
||
// ...
|
||
}
|
||
|
||
export default DeleteUser
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="fetch" label="Fetch API">
|
||
|
||
```ts
|
||
fetch(`<BACKEND_URL>/admin/users/${userId}`, {
|
||
credentials: "include",
|
||
method: "DELETE",
|
||
})
|
||
.then((response) => response.json())
|
||
.then(({ id, object, deleted }) => {
|
||
console.log(id)
|
||
})
|
||
```
|
||
|
||
</TabItem>
|
||
<TabItem value="curl" label="cURL">
|
||
|
||
```bash
|
||
curl -L -X DELETE '<BACKEND_URL>/admin/users/<USER_ID>' \
|
||
-H 'Authorization: Bearer <API_TOKEN>'
|
||
```
|
||
|
||
</TabItem>
|
||
</Tabs>
|
||
|
||
This endpoint requires the user ID as a path parameter.
|
||
|
||
It deletes the user and returns the following fields:
|
||
|
||
- `id`: The ID of the deleted user.
|
||
- `object`: The type of object that was deleted. In this case, the value will be `user`.
|
||
- `deleted`: A boolean value indicating whether the user was deleted.
|
||
|
||
---
|
||
|
||
## See Also
|
||
|
||
- [How to manage a user’s profile](./manage-profile.mdx)
|