Skip to content

Commit d8d8a48

Browse files
committed
fix: B2B-3725 add required b2b headers
1 parent 5c73ac0 commit d8d8a48

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
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: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ interface LoginWithB2BParams {
1212
const ENV = z
1313
.object({
1414
env: z.object({
15-
B2B_API_TOKEN: z.string(),
1615
BIGCOMMERCE_CHANNEL_ID: z.string(),
16+
BIGCOMMERCE_STORE_HASH: z.string(),
17+
BIGCOMMERCE_ACCESS_TOKEN: z.string().optional(),
18+
B2B_API_TOKEN: z.string().optional().describe('This is deprecated in favour or BIGCOMMERCE_ACCESS_TOKEN, read https://support.bigcommerce.com/s/article/Store-API-Accounts?language=en_US'),
1719
}),
1820
})
1921
.transform(({ env }) => env);
@@ -29,17 +31,26 @@ const B2BTokenResponseSchema = z.object({
2931
});
3032

3133
export async function loginWithB2B({ customerId, customerAccessToken }: LoginWithB2BParams) {
32-
const { B2B_API_TOKEN, BIGCOMMERCE_CHANNEL_ID } = ENV.parse(process);
34+
const { BIGCOMMERCE_CHANNEL_ID, B2B_API_TOKEN, BIGCOMMERCE_STORE_HASH, BIGCOMMERCE_ACCESS_TOKEN } = ENV.parse(process);
35+
const headers: HeadersInit = {
36+
Accept: 'application/json',
37+
'Content-Type': 'application/json',
38+
};
3339

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

50+
const apiHost = getAPIHostname();
3651
const response = await fetch(`${apiHost}/api/io/auth/customers/storefront`, {
3752
method: 'POST',
38-
headers: {
39-
Accept: 'application/json',
40-
'Content-Type': 'application/json',
41-
authToken: B2B_API_TOKEN,
42-
},
53+
headers,
4354
body: JSON.stringify({
4455
channelId: BIGCOMMERCE_CHANNEL_ID,
4556
customerId,

0 commit comments

Comments
 (0)