|
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: `Retrieve Logged-In Customer in Storefront`, |
|
15
|
} |
|
16
|
|
|
17
|
# {metadata.title} |
|
18
|
|
|
19
|
In this guide, you'll learn how to retrieve a customer after they've been authenticated in your storefront. |
|
20
|
|
|
21
|
## Prerequisites: Set the Customer's Authentication Token |
|
22
|
|
|
23
|
When using the [JS SDK](../../../js-sdk/page.mdx), make sure that you've set the customer's authentication token using the `setToken` function: |
|
24
|
|
|
25
|
```ts |
|
26
|
sdk.client.setToken(token) |
|
27
|
``` |
|
28
|
|
|
29
|
You can learn more in the [Login Customer](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides. |
|
30
|
|
|
31
|
--- |
|
32
|
|
|
33
|
## Retrieve Logged-In Customer |
|
34
|
|
|
35
|
To retrieve the logged-in customer, send a request to the [Get Customer API route](!api!/store/customers/get-customer): |
|
36
|
|
|
37
|
```ts |
|
38
|
sdk.store.customer.retrieve() |
|
39
|
.then(({ customer }) => { |
|
40
|
// use customer... |
|
41
|
console.log(customer) |
|
42
|
}) |
|
43
|
``` |
|
44
|
|
|
45
|
This will retrieve the authenticated customer's details. The [Get Customer API route](!api!/store/customers/get-customer) returns a `customer` field, which is a [customer object](!api!/store/customers/schema). |
|
46
|
|
|
47
|
Notice that the JS SDK automatically passes the necessary authentication headers or cookies based on your authentication configurations, as explained in the [Login Customer](../login/page.mdx) guide. |
|
48
|
|
|
49
|
If you're not using the JS SDK, you need to pass the authentication token in the request headers or cookies accordingly: |
|
50
|
|
|
51
|
- If you authenticate the customer with bearer authorization, pass the token in the authorization header of the request. |
|
52
|
- If you authenticate the customer with cookie session, pass the `credentials: include` option to the `fetch` function. |
|
53
|
|
|
54
|
--- |
|
55
|
|
|
56
|
## Restrict Access to Authenticated Customers |
|
57
|
|
|
58
|
In your storefront, it's common to restrict access to certain pages to authenticated customers only. |
|
59
|
|
|
60
|
For example, you may want to restrict access to the customer's profile page to authenticated customers only. |
|
61
|
|
|
62
|
To do this, you can try to retrieve the customer's details. If the request fails, you can redirect the customer to the login page. |
|
63
|
|
|
64
|
For example: |
|
65
|
|
|
66
|
```ts |
|
67
|
sdk.store.customer.retrieve() |
|
68
|
.then(({ customer }) => { |
|
69
|
// use customer... |
|
70
|
console.log(customer) |
|
71
|
}) |
|
72
|
.catch(() => { |
|
73
|
// redirect to login page |
|
74
|
}) |
|
75
|
``` |
|
76
|
|
|
77
|
The `catch` block will only execute if the request fails, which means that the customer is not authenticated. You can add the redirect logic to the `catch` block based on your storefront framework. |
|
78
|
|
|
79
|
<Note title="Tip"> |
|
80
|
|
|
81
|
If you're building a React storefront, you can use the `useCustomer` hook defined in the [Customer Context guide](../context/page.mdx) to check if the customer is set. If not, you can redirect the customer to the login page. |
|
82
|
|
|
83
|
</Note> |