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.
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#
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#
actor
stringuser
for admin user, or customer
for customer.method
stringemailpass
or google
.payload
AdminSignInWithEmailPassword | 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.
payload
AdminSignInWithEmailPassword | Record<string, unknown>emailpass
provider,
you pass the email and password.Returns#
Promise
Promise<string | object>The authentication JWT token
Promise
Promise<string | object>