Skip to content

Commit ab06c1b

Browse files
authored
fix: B2B-3725 add required b2b headers (#2631)
1 parent 5c73ac0 commit ab06c1b

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

.env.example

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ DEFAULT_REVALIDATE_TARGET=3600
4444
# URL for the B2B API. This is used to connect to the B2B API for features like customer impersonation and B2B-specific data
4545
B2B_API_HOST=https://api-b2b.bigcommerce.com
4646

47-
# The B2B API Token is used to authenticate requests to the B2B API.
48-
# It can be generated in the B2B control panel Settings > API Accounts > Create API Account.
49-
B2B_API_TOKEN=
47+
# A store-level API account token used for REST API actions. Optional by default, but required in
48+
# the sign-in logic that interacts with the buyer portal. This integration requires a
49+
# `BIGCOMMERCE_ACCESS_TOKEN` with `modify` scope on B2B Edition.
50+
# See https://support.bigcommerce.com/s/article/Store-API-Accounts?language=en_US
51+
BIGCOMMERCE_ACCESS_TOKEN=
5052

5153
# URL for the local buyer portal instance. Uncomment if developing locally.
5254
# LOCAL_BUYER_PORTAL_HOST=http://localhost:3001

core/b2b/client.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ interface LoginWithB2BParams {
1111

1212
const ENV = z
1313
.object({
14-
env: z.object({
15-
B2B_API_TOKEN: z.string(),
16-
BIGCOMMERCE_CHANNEL_ID: z.string(),
17-
}),
14+
env: z.union([
15+
z.object({
16+
BIGCOMMERCE_CHANNEL_ID: z.string(),
17+
B2B_API_TOKEN: z.string(),
18+
}),
19+
z.object({
20+
BIGCOMMERCE_CHANNEL_ID: z.string(),
21+
BIGCOMMERCE_STORE_HASH: z.string(),
22+
BIGCOMMERCE_ACCESS_TOKEN: z.string(),
23+
}),
24+
]),
1825
})
1926
.transform(({ env }) => env);
2027

@@ -29,17 +36,27 @@ const B2BTokenResponseSchema = z.object({
2936
});
3037

3138
export async function loginWithB2B({ customerId, customerAccessToken }: LoginWithB2BParams) {
32-
const { B2B_API_TOKEN, BIGCOMMERCE_CHANNEL_ID } = ENV.parse(process);
39+
const env = ENV.parse(process);
40+
const BIGCOMMERCE_CHANNEL_ID = env.BIGCOMMERCE_CHANNEL_ID;
41+
const headers: HeadersInit = {
42+
Accept: 'application/json',
43+
'Content-Type': 'application/json',
44+
};
3345

34-
const apiHost = getAPIHostname();
46+
if ('BIGCOMMERCE_ACCESS_TOKEN' in env) {
47+
headers['X-Auth-Token'] = env.BIGCOMMERCE_ACCESS_TOKEN;
48+
headers['X-Store-Hash'] = env.BIGCOMMERCE_STORE_HASH;
49+
} else if ('B2B_API_TOKEN' in env) {
50+
headers['authToken'] = env.B2B_API_TOKEN;
51+
console.warn('This is deprecated in favour or BIGCOMMERCE_ACCESS_TOKEN, read https://support.bigcommerce.com/s/article/Store-API-Accounts?language=en_US')
52+
} else {
53+
throw new Error('No B2B API token or BigCommerce token found in environment variables.');
54+
}
3555

56+
const apiHost = getAPIHostname();
3657
const response = await fetch(`${apiHost}/api/io/auth/customers/storefront`, {
3758
method: 'POST',
38-
headers: {
39-
Accept: 'application/json',
40-
'Content-Type': 'application/json',
41-
authToken: B2B_API_TOKEN,
42-
},
59+
headers,
4360
body: JSON.stringify({
4461
channelId: BIGCOMMERCE_CHANNEL_ID,
4562
customerId,

0 commit comments

Comments
 (0)