www/apps/resources/app/storefront-development/customers/log-out/page.mdx
import { CodeTabs, CodeTab } from "docs-ui"
export const metadata = {
title: Log-out Customer in Storefront,
}
In this guide, you'll learn how to log-out a customer in the storefront based on the authentication method.
If you're using the JS SDK, you can use the auth.logout method to log-out the customer:
sdk.auth.logout()
.then(() => {
// TODO redirect customer to login page
})
The JS SDK will handle the necessary actions based on the authentication method you're using:
session authentication method, the JS SDK will send a DELETE request to the /auth/session route. Then, it will remove any stored tokens from the configured storage method (by default, localStorage).jwt authentication method, the JS SDK will only remove the JWT token from the configured storage method (by default, localStorage).Once the operation succeeds, you can redirect the customer to the login page.
If you're not using the JS SDK, you need to log out the customer based on the authentication method you're using.
If you're authenticating the customer with their JWT token, remove the JWT token stored locally in your storefront based on your storage method.
For example, if you're storing the JWT token in localStorage, remove the item from it:
localStorage.removeItem(`token`)
Where token is the key of the JWT token in the localStorage.
If you're authenticating the customer with their cookie session ID, you need to send a DELETE request to the /auth/session route. This will remove the session cookie from the customer's browser.
For example:
fetch(`http://localhost:9000/auth/session`, {
credentials: "include",
method: "DELETE",
})
.then((res) => res.json())
.then(() => {
// TODO redirect customer to login page
})
The API route returns nothing in the response. If the request was successful, you can perform any necessary work to unset the customer and redirect them to the login page.