Login Customer in the Storefront
Log a customer in with email and password.
On this page
In this guide, you'll learn about the two ways to login a customer in your storefront.
Next.js App Router (recommended)
Do not store the JWT in localStorage. After sdk.store.auth.login, write the token to the _aiminaabee_jwt httpOnly cookie, revalidate the customers cache tag, and transfer the guest cart.
ts
setAuthToken sets _aiminaabee_jwt (httpOnly, sameSite: "strict", 7-day maxAge, secure in production). transferCart calls sdk.store.cart.transferCart with the _aiminaabee_cart_id cookie so guest line items move to the customer.
See Build a Next.js Storefront. The rest of this page covers JWT vs session and SPA examples.
Alternative Guides: - This guide covers login using email and password. For authentication with third-party providers, refer to the Third-Party Login guide.
- If you have email verification enabled, refer to the Verify Account guide instead.
Login Customer Methods
There are two ways to login a customer in your storefront:
- Using a JWT token. This JWT token is obtained from the
/auth/customer/emailpassAPI route and is used as a bearer token in the authorization header of all requests. - Using a cookie session. This method uses the
/auth/sessionAPI route to set the authenticated session ID in the cookies.
The JS SDK simplifies the login approach in a single auth.login method. The upcoming sections explain the authentication approach whether you're using the JS SDK or not.
Which Authentication Method Should You Use?
The authentication method you choose depends on your use case and the type of storefront you're building.
Refer to the JS SDK Authentication guide to learn more about the differences between JWT and session authentication and which one is best for your use case.
JS SDK Authentication Configuration
Before implementing the login flow, you need to configure in the JS SDK the authentication method you're using in your storefront. This defines how the JS SDK will handle sending authenticated requests after the customer is authenticated.
For example, add the following configuration to your JS SDK initialization:
Tip: Learn how to install and configure the JS SDK in the Getting started guide.
JWT
ts
Session
ts
The JS SDK will now pass the JWT token or the session ID cookie in the authorization header of all subsequent requests based on the authentication method you've configured.
Refer to the JS SDK Authentication guide for more information about these configurations, as well as other authentication configurations.
Tip: The JS SDK's default JWT storage is localStorage. On Next.js App Router, skip that default: store the token in _aiminaabee_jwt as shown above. For React Native, use custom SDK storage as described in JS SDK Authentication.
Authentication with JS SDK
The JS SDK provides an auth.login method that handles all authentication steps based on the configured authentication method. Then, all subsequent requests will have the necessary authentication headers or cookies.
For example, to implement the login flow in your storefront with the JS SDK:
React
tsx
JS SDK
ts
In the example above, you:
- Create a
handleLoginfunction that logs in a customer. - In the function, you log in the customer using the
sdk.auth.loginmethod.- If an error occurs, show an alert and exit execution.
- The method may return an object with a
locationproperty. This occurs when using third-party authentication providers. Learn more about implementing third-party authentication in the Third-Party Login guide. - Otherwise, the authentication was successful.
- All subsequent requests are now authenticated. As an example, you send a request to obtain the logged-in customer's details.
Authentication without JS SDK
If you're not using the JS SDK, the next sections cover the general flow for authenticating a customer in your storefront for both methods.
1. Using a JWT Token
The first authentication approach is to pass an authenticated JWT token in the authorization header of all requests. You can do that by:
- Retrieving a JWT token from the
/auth/customer/emailpassAuthenticate Customer API route:
bash
- Passing the token in the authorization header of all subsequent requests, as explained in the API reference:
bash
Store the JWT based on your platform. On Next.js, use the _aiminaabee_jwt httpOnly cookie. SPAs may use memory or sessionStorage. Avoid localStorage for tokens when you can.
2. Using a Cookie Session
The second authentication approach is to authenticate the customer with a cookie session. You do that by:
- Retrieving a JWT token from the
/auth/customer/emailpassAuthenticate Customer API route:
bash
- Sending a request to the
/auth/sessionAuthentication Session API route passing in the authorization header the token as a Bearer token. This sets the authenticated session ID in the cookies:
bash
- Passing the cookie session ID in all subsequent requests:
cURL
bash
Fetch
ts