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.

Log-out using the JS SDK#

If you're using the JS SDK, you can use the auth.logout method to log-out the customer:

Code
1sdk.auth.logout()2.then(() => {3  // TODO redirect customer to login page4})

The JS SDK will handle the necessary actions based on the authentication method you're using:

  1. If you're using the 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).
  2. If you're using the 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.


Log-out without the JS SDK#

If you're not using the JS SDK, you need to log out the customer based on the authentication method you're using.

Log-out for JWT Token#

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:

Code
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:

Code
1fetch(`http://localhost:9000/auth/session`, {2  credentials: "include",3  method: "DELETE",4})5.then((res) => res.json())6.then(() => {7  // TODO redirect customer to login page8})

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.

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