|
1
|
--- |
|
2
|
tags: |
|
3
|
- customer |
|
4
|
- storefront |
|
5
|
- auth |
|
6
|
products: |
|
7
|
- customer |
|
8
|
- auth |
|
9
|
--- |
|
10
|
|
|
11
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
12
|
|
|
13
|
export const metadata = { |
|
14
|
title: `Log-out Customer in Storefront`, |
|
15
|
} |
|
16
|
|
|
17
|
# {metadata.title} |
|
18
|
|
|
19
|
In this guide, you'll learn how to log-out a customer in the storefront based on the authentication method. |
|
20
|
|
|
21
|
## Log-out using the JS SDK |
|
22
|
|
|
23
|
If you're using the JS SDK, you can use the `auth.logout` method to log-out the customer: |
|
24
|
|
|
25
|
```ts |
|
26
|
sdk.auth.logout() |
|
27
|
.then(() => { |
|
28
|
// TODO redirect customer to login page |
|
29
|
}) |
|
30
|
``` |
|
31
|
|
|
32
|
The JS SDK will handle the necessary actions based on the [authentication method](../login/page.mdx#js-sdk-authentication-configuration) you're using: |
|
33
|
|
|
34
|
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`). |
|
35
|
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`). |
|
36
|
|
|
37
|
Once the operation succeeds, you can redirect the customer to the login page. |
|
38
|
|
|
39
|
--- |
|
40
|
|
|
41
|
## Log-out without the JS SDK |
|
42
|
|
|
43
|
If you're not using the JS SDK, you need to log out the customer based on the authentication method you're using. |
|
44
|
|
|
45
|
### Log-out for JWT Token |
|
46
|
|
|
47
|
If you're authenticating the customer with their JWT token, remove the JWT token stored locally in your storefront based on your storage method. |
|
48
|
|
|
49
|
For example, if you're storing the JWT token in `localStorage`, remove the item from it: |
|
50
|
|
|
51
|
```ts |
|
52
|
localStorage.removeItem(`token`) |
|
53
|
``` |
|
54
|
|
|
55
|
Where `token` is the key of the JWT token in the `localStorage`. |
|
56
|
|
|
57
|
### Log-out for Cookie Session |
|
58
|
|
|
59
|
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. |
|
60
|
|
|
61
|
For example: |
|
62
|
|
|
63
|
```ts |
|
64
|
fetch(`http://localhost:9000/auth/session`, { |
|
65
|
credentials: "include", |
|
66
|
method: "DELETE", |
|
67
|
}) |
|
68
|
.then((res) => res.json()) |
|
69
|
.then(() => { |
|
70
|
// TODO redirect customer to login page |
|
71
|
}) |
|
72
|
``` |
|
73
|
|
|
74
|
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. |