login - JS SDK Auth Reference

This documentation provides a reference to the sdk.auth.login method used to send requests to Medusa's Authentication API routes. It can be used for admin users, customers, or custom actor types.

This method retrieves the JWT authenticated token for an admin user, customer, or custom actor type. It sends a request to the Authenticate API Route.

Third-Party Authentication#

If the API route returns a location property, it means that the authentication requires additional steps, typically in a third-party service. The location property is returned so that you can redirect the user to the appropriate page.

Note: For an example of implementing third-party authentication, refer to the Third-Party Login in Storefront guide.

Session Authentication#

If the auth.type of the SDK is set to session, this method will also send a request to the Set Authentication Session API route.

Learn more in the JS SDK Authentication guide.

Automatic Authentication#

If the authentication was successful, subsequent requests using the SDK will automatically have the necessary authentication headers / session set, based on your JS SDK authentication configurations.

Learn more in the JS SDK Authentication guide.

Example#

Code
1const result = await sdk.auth.login(2  "customer",3  "emailpass",4  {5    email: "customer@gmail.com",6    password: "supersecret"7  }8)9
10if (typeof result !== "string") {11  alert("Authentication requires additional steps")12  // replace with the redirect logic of your application13  window.location.href = result.location14  return15}16
17// customer is now authenticated18// all subsequent requests will use the token in the header19const { customer } = await sdk.store.customer.retrieve()

Parameters#

actorstring
The actor type. For example, user for admin user, or customer for customer.
methodstring
The authentication provider to use. For example, emailpass or google.
payloadAdminSignInWithEmailPassword | Record<string, unknown>
The data to pass in the request's body for authentication. When using the emailpass provider, you pass the email and password.

Returns#

PromisePromise<string | object>
The authentication JWT token
Was this page helpful?
Ask Anything
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