How to Use Authentication Routes
In this document, you'll learn about the authentication routes and how to use them to create and log-in users, and reset their password.
Types of Authentication Flows#
1. Basic Authentication Flow#
This authentication flow doesn't require validation with third-party services.
The steps are:

- Register the user with the Register Route.
- Use the authentication token to create the user with their respective API route.
- For example, for customers you would use the Create Customer API route.
- For admin users, you accept an invite using the Accept Invite API route
- Authenticate the user with the Auth Route.
After registration, you only use the Auth Route for subsequent authentication.
2. Third-Party Service Authenticate Flow#
This authentication flow authenticates the user with a third-party service, such as Google.
It requires the following steps:

- Authenticate the user with the Auth Route.
- The auth route returns a URL to authenticate with third-party service, such as login with Google. The frontend (such as a storefront), when it receives a
locationproperty in the response, must redirect to the returned location. - Once the authentication with the third-party service finishes, it redirects back to the frontend with a
codequery parameter. So, make sure your third-party service is configured to redirect to your frontend page after successful authentication. - The frontend sends a request to the Validate Callback Route passing it the query parameters received from the third-party service, such as the
codeandstatequery parameters. - If the callback validation is successful, the frontend receives the authentication token.
- Decode the received token in the frontend using tools like react-jwt.
- If the decoded data has an
actor_idproperty, then the user is already registered. So, use this token for subsequent authenticated requests. - If not, follow the rest of the steps.
- If the decoded data has an
- The frontend uses the authentication token to create the user with their respective API route.
- For example, for customers you would use the Create Customer API route.
- For admin users, you accept an invite using the Accept Invite API route
- The frontend sends a request to the Refresh Token Route to retrieve a new token with the user information populated.
Register Route#
The Medusa application defines an API route at /auth/{actor_type}/{provider}/register that creates an auth identity for an actor type, such as a customer. It returns a JWT token that you pass to an API route that creates the user.
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/register
-H 'Content-Type: application/json' \
--data-raw '{
"email": "Whitney_Schultz@gmail.com"
// ...
}'
emailpass that uses custom logic to authenticate a user. For authentication providers that authenticate with third-party services, such as Google, use the Auth Route instead.For example, if you're registering a customer, you:
- Send a request to
/auth/customer/emailpass/registerto retrieve the registration JWT token. - Send a request to the Create Customer API route to create the customer, passing the JWT token in the header.
Path Parameters#
Its path parameters are:
- : the actor type of the user you're authenticating. For example, .
- : the auth provider to handle the authentication. For example, .
Request Body Parameters#
This route accepts in the request body the data that the specified authentication provider requires to handle authentication.
For example, the EmailPass provider requires an email and password fields in the request body.
Response Fields#
If the authentication is successful, you'll receive a token field in the response body object:
Use that token in the header of subsequent requests to send authenticated requests.
Handling Existing Identities#
An auth identity with the same email may already exist in Medusa. This can happen if:
- Another actor type is using that email. For example, an admin user is trying to register as a customer.
- The same email belongs to a record of the same actor type. For example, another customer has the same email.
In these scenarios, the Register Route will return an error instead of a token:
To handle these scenarios, you can use the Login Route to validate that the email and password match the existing identity. If so, you can allow the admin user, for example, to register as a customer.
Otherwise, if the email and password don't match the existing identity, such as when the email belongs to another customer, the Login Route returns an error:
You can show that error message to the customer.
Login Route#
The Medusa application defines an API route at /auth/{actor_type}/{provider} that authenticates a user of an actor type. It returns a JWT token that can be passed in the header of subsequent requests to send authenticated requests.
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}
-H 'Content-Type: application/json' \
--data-raw '{
"email": "Whitney_Schultz@gmail.com"
// ...
}'
For example, if you're authenticating a customer, you send a request to /auth/customer/emailpass.
Path Parameters#
Its path parameters are:
{actor_type}: the actor type of the user you're authenticating. For example,customer.{provider}: the auth provider to handle the authentication. For example,emailpass.
Request Body Parameters#
This route accepts in the request body the data that the specified authentication provider requires to handle authentication.
For example, the EmailPass provider requires an email and password fields in the request body.
Overriding Callback URL
For the GitHub and Google providers, you can pass a callback_url body parameter that overrides the callbackUrl set in the provider's configurations.
This is useful if you want to redirect the user to a different URL after authentication based on their actor type. For example, you can set different callback_url for admin users and customers.
Response Fields#
If the authentication is successful, you'll receive a field in the response body object:
Use that token in the header of subsequent requests to send authenticated requests.
If the authentication requires more action with a third-party service, you'll receive a location property:
Redirect to that URL in the frontend to continue the authentication process with the third-party service.
Customizing the Response Body
A custom auth provider can't change the shape of this route's response body. The provider's authenticate method returns an AuthenticationResponse object, but this route only uses its success, authIdentity, location, and error fields to build the response. Any other fields you return from the provider are ignored.
If you need to return a custom response body, create a custom API route that calls the Auth Module's authenticate method, then shape the response as you need.
Validate Callback Route#
The Medusa application defines an API route at /auth/{actor_type}/{provider}/callback that's useful for validating the authentication callback or redirect from third-party services like Google.
Path Parameters#
Its path parameters are:
{actor_type}: the actor type of the user you're authenticating. For example,customer.{provider}: the auth provider to handle the authentication. For example,google.
Query Parameters#
This route accepts all the query parameters that the third-party service sends to the frontend after the user completes the authentication process, such as the code and state query parameters.
Response Fields#
If the authentication is successful, you'll receive a token field in the response body object:
In your frontend, decode the token using tools like react-jwt:
- If the decoded data has an
actor_idproperty, the user is already registered. So, use this token for subsequent authenticated requests. - If not, use the token in the header of a request that creates the user, such as the Create Customer API route.
The decoded data may look like this:
{
"actor_id": "", // Empty if the user is not registered
"user_metadata": {
"email": "Whitney_Schultz@gmail.com"
}
// other fields...
}
Access the Provider's Data After Validation#
validateAuthCallbackWorkflow and its authCallbackValidated hook are available since Medusa v2.23.1.The Validate Callback Route runs the validateAuthCallbackWorkflow to validate the callback with the provider.
The workflow has an authCallbackValidated hook that runs after the provider validates the callback and before the route issues the token. The hook receives the data that the provider received from the third-party service, such as access and refresh tokens. So, you can consume the hook to store that data in your own storage. For example, to call the GitHub API on the customer's behalf later.
Consume the hook in a file under src/workflows/hooks:
import {
validateAuthCallbackWorkflow,
} from "@medusajs/medusa/core-flows"
validateAuthCallbackWorkflow.hooks.authCallbackValidated(
async (
{ auth_identity, auth_provider, provider_data },
{ container }
) => {
if (auth_provider !== "github") {
return
}
// TODO store provider_data.token, encrypted,
// in your own storage
}
)
The hook's handler receives an object with the following properties:
actor_type: The type of actor logging in, such ascustomer.auth_provider: The ID of the auth provider, such asgithub.auth_identity: The auth identity that logged in.mfa_required: Whether the user still needs to complete an MFA challenge.provider_data: The data that the provider received from the third-party service, keyed by the call that returned it. Its keys differ for each provider:
provider_data is secret, as it can hold access, refresh, and ID tokens. Don't log it, and don't pass it to a long-running workflow, which stores its execution. The workflow never returns provider_data in its result, and the route never sends it to the client.If the hook's handler throws an error, the login fails. The login can also still fail after the hook runs. For example, the route refuses the login if the auth identity isn't associated with an actor of the specified type and the actor type requires an association.
Learn more about consuming hooks in the Workflow Hooks chapter.
Refresh Token Route#
The Medusa application defines an API route at /auth/token/refresh that's useful after authenticating a user with a third-party service to populate the user's token with their new information.
It requires the user's JWT token that they received from the authentication or callback routes.
Response Fields#
If the token was refreshed successfully, you'll receive a token field in the response body object:
Use that token in the header of subsequent requests to send authenticated requests.
Tokens Issued Before a Credentials Update#
The route refuses to refresh a token that was issued before the user's credentials, such as their password, were last updated. In that case, the route returns an Unauthorized error with the message The token was issued before the credentials were updated, and the user must log in again to receive a new token.
For example, when a user resets their password with the Reset Password Route, tokens issued before the reset can no longer be refreshed.
Medusa retrieves the date of the last credentials update from the auth provider that issued the token. The Emailpass Auth Module Provider tracks when a user's password was last updated. If you're building a custom auth provider, refer to the Create Auth Provider guide to learn how to implement the getCredentialsUpdatedAt method.
Reset Password Routes#
To reset a user's password:
- Generate a token using the Generate Reset Password Token API route.
- The API route emits the
auth.password_resetevent, passing the token in the payload. - You can create a subscriber, as seen in this guide, that listens to the event and send a notification to the user.
- The API route emits the
- Pass the token to the Reset Password API route to reset the password.
- The URL in the user's notification should direct them to a frontend URL, which sends a request to this route.
Generate Reset Password Token Route#
The Medusa application defines an API route at /auth/{actor_type}/{auth_provider}/reset-password that emits the auth.password_reset event, passing the token in the payload.
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/reset-password
-H 'Content-Type: application/json' \
--data-raw '{
"identifier": "Whitney_Schultz@gmail.com"
}'
emailpass that store a user's password and use it for authentication.Path Parameters
Its path parameters are:
- : the actor type of the user you're authenticating. For example, .
- : the auth provider to handle the authentication. For example, .
Request Body Parameters
This route accepts in the request body an object having the following property:
identifier: The user's identifier in the specified auth provider. For example, for theemailpassauth provider, you pass the user's email.
Response Fields
If the authentication is successful, the request returns a 201 response code.
Reset Password Route#
The Medusa application defines an API route at /auth/{actor_type}/{auth_provider}/update that accepts a token and, if valid, updates the user's password.
curl -X POST http://localhost:9000/auth/{actor_type}/{providers}/update
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {token}' \
--data-raw '{
"email": "Whitney_Schultz@gmail.com",
"password": "supersecret"
}'
emailpass that store a user's password and use it for logging them in.Path Parameters
Its path parameters are:
{actor_type}: the actor type of the user you're authenticating. For example,customer.{provider}: the auth provider to handle the authentication. For example,emailpass.
Pass Token in Authorization Header
Authorization header.In the request's authorization header, you must pass the token generated using the Generate Reset Password Token route. You pass it as a bearer token.
Request Body Parameters
This route accepts in the request body an object that has the data necessary for the provider to update the user's password.
For the emailpass provider, you must pass the following properties:
email: The user's email.password: The new password.
Response Fields#
If the authentication is successful, the request returns an object with a success property set to true: