User Creation Flows

In this document, learn the different ways to create a user using the User Module.

Looking for no-code docs? Refer to this Medusa Admin User Guide to learn how to manage users using the dashboard.

Straightforward User Creation#

To create a user, use the createUsers method of the User Module’s main service:

Code
1const user = await userModuleService.createUsers({2  email: "user@example.com",3})

You can pair this with the Auth Module to allow the user to authenticate, as explained in a later section.


Invite Users#

To create a user, you can create an invite for them using the createInvites method of the User Module's main service:

Code
1const invite = await userModuleService.createInvites({2  email: "user@example.com",3})

Later, you can accept the invite and create a new user for them:

Code
1const invite =2  await userModuleService.validateInviteToken("secret_123")3
4await userModuleService.updateInvites({5  id: invite.id,6  accepted: true,7})8
9const user = await userModuleService.createUsers({10  email: invite.email,11})

Invite Expiry#

An invite has an expiry date. You can renew the expiry date and refresh the token using the refreshInviteTokens method:

Code
await userModuleService.refreshInviteTokens(["invite_123"])

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.

So, when a user is authenticated, and you receive the AuthIdentity object, you can use it to create a user if it doesn’t exist:

Code
1const { success, authIdentity } =2  await authModuleService.authenticate("emailpass", {3    // ...4  })5
6const [, count] = await userModuleService.listAndCountUsers({7  email: authIdentity.entity_id,8})9
10if (!count) {11  const user = await userModuleService.createUsers({12    email: authIdentity.entity_id,13  })14}
Was this page helpful?
Ask Anything
Ask any questions about Medusa. Get help with your development.
You can also use the Medusa MCP server in Cursor, VSCode, etc...
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break