Skip to main content
Skip to main content

Users

Queries and Mutations listed here are used to send requests to the Admin User API Routes.

All hooks listed require authentication.

A store can have more than one user, each having the same privileges. Admins can manage users, their passwords, and more.

Related Guide: How to manage users.

Mutations

useAdminCreateUser

This hook creates an admin user. The user has the same privileges as all admin users, and will be able to authenticate and perform admin functionalities right after creation.

Example

import React from "react"
import { useAdminCreateUser } from "medusa-react"

const CreateUser = () => {
const createUser = useAdminCreateUser()
// ...

const handleCreateUser = () => {
createUser.mutate({
email: "user@example.com",
password: "supersecret",
}, {
onSuccess: ({ user }) => {
console.log(user.id)
}
})
}

// ...
}

export default CreateUser

Mutation Function Parameters

AdminCreateUserPayloadAdminCreateUserPayloadRequired

Mutation Function Returned Data

AdminUserResAdminUserResRequired
The user's details.

useAdminUpdateUser

This hook updates an admin user's details.

Example

import React from "react"
import { useAdminUpdateUser } from "medusa-react"

type Props = {
userId: string
}

const User = ({ userId }: Props) => {
const updateUser = useAdminUpdateUser(userId)
// ...

const handleUpdateUser = (
firstName: string
) => {
updateUser.mutate({
first_name: firstName,
}, {
onSuccess: ({ user }) => {
console.log(user.first_name)
}
})
}

// ...
}

export default User

Hook Parameters

idstringRequired
The user's ID.

Mutation Function Parameters

AdminUpdateUserPayloadAdminUpdateUserPayloadRequired

Mutation Function Returned Data

AdminUserResAdminUserResRequired
The user's details.

useAdminDeleteUser

This hook deletes a user. Once deleted, the user will not be able to authenticate or perform admin functionalities.

Example

import React from "react"
import { useAdminDeleteUser } from "medusa-react"

type Props = {
userId: string
}

const User = ({ userId }: Props) => {
const deleteUser = useAdminDeleteUser(userId)
// ...

const handleDeleteUser = () => {
deleteUser.mutate(void 0, {
onSuccess: ({ id, object, deleted }) => {
console.log(id)
}
})
}

// ...
}

export default User

Hook Parameters

idstringRequired
The user's ID.

Mutation Function Returned Data

DeleteResponseDeleteResponseRequired
The response returned for a DELETE request.

useAdminResetPassword

This hook resets the password of an admin user using their reset password token. You must generate a reset password token first for the user using the useAdminSendResetPasswordToken hook, then use that token to reset the password in this hook.

Example

import React from "react"
import { useAdminResetPassword } from "medusa-react"

const ResetPassword = () => {
const resetPassword = useAdminResetPassword()
// ...

const handleResetPassword = (
token: string,
password: string
) => {
resetPassword.mutate({
token,
password,
}, {
onSuccess: ({ user }) => {
console.log(user.id)
}
})
}

// ...
}

export default ResetPassword

Mutation Function Parameters

AdminResetPasswordRequestAdminResetPasswordRequestRequired
The details of the password reset request.

Mutation Function Returned Data

AdminUserResAdminUserResRequired
The user's details.

useAdminSendResetPasswordToken

This hook generates a password token for an admin user with a given email. This also triggers the user.password_reset event. So, if you have a Notification Service installed that can handle this event, a notification, such as an email, will be sent to the user. The token is triggered as part of the user.password_reset event's payload. That token must be used later to reset the password using the useAdminResetPassword hook.

Example

import React from "react"
import { useAdminSendResetPasswordToken } from "medusa-react"

const Login = () => {
const requestPasswordReset = useAdminSendResetPasswordToken()
// ...

const handleResetPassword = (
email: string
) => {
requestPasswordReset.mutate({
email
}, {
onSuccess: () => {
// successful
}
})
}

// ...
}

export default Login

Mutation Function Parameters

AdminResetPasswordTokenRequestAdminResetPasswordTokenRequestRequired
The details of the password reset token request.

Queries

useAdminUsers

This hook retrieves all admin users.

Example

To list users:

import React from "react"
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

By default, only the first 20 records are retrieved. You can control pagination by specifying the limit and offset properties:

import React from "react"
import { useAdminUsers } from "medusa-react"

const Users = () => {
const {
users,
limit,
offset,
isLoading
} = useAdminUsers({
limit: 20,
offset: 0
})

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

Hook Parameters

Query Returned Data

limitnumberRequired
The limit applied on the retrieved items.
offsetnumberRequired
The number of items skipped before retrieving the list of items.
countnumberRequired
The total count of items.
usersOmit<User, "password_hash">[]Required
An array of users details.

useAdminUser

This hook retrieves an admin user's details.

Example

import React from "react"
import { useAdminUser } from "medusa-react"

type Props = {
userId: string
}

const User = ({ userId }: Props) => {
const { user, isLoading } = useAdminUser(
userId
)

return (
<div>
{isLoading && <span>Loading...</span>}
{user && <span>{user.first_name} {user.last_name}</span>}
</div>
)
}

export default User

Hook Parameters

idstringRequired
The user's ID.

Query Returned Data

userOmit<User, "password_hash">Required
User details.
Was this section helpful?