Skip to content

Commit

Permalink
Update SLAS helper to require channel_id (#165)
Browse files Browse the repository at this point in the history
* update SLAS helper to require channel_id

* move channel_id check earlier in function

* update changelog

* add planned changes description

* add unit test
  • Loading branch information
joeluong-sfcc authored Jul 16, 2024
1 parent 3853532 commit 1e893c7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# CHANGELOG

## v3.0.0

### :warning: Planned Shopper Context Changes :warning:

Starting July 31st 2024, all endpoints in the Shopper context API will require the `siteId` parameter for new customers. This field is marked as optional for backward compatibility and will be changed to mandatory tentatively by January 2025.

### Enchancements

- Update SLAS helper function `loginGuestUserPrivate` to require `channel_id` as SLAS requires `channel_id` when requesting a guest access token with a `grant_type` of `client_credentials` starting July 31st 2024 [#165](https://github.com/SalesforceCommerceCloud/commerce-sdk-isomorphic/pull/165)
- See the [announcement on the developer docs](https://developer.salesforce.com/docs/commerce/commerce-api/guide/slas.html#guest-tokens) for more information

## v2.1.0

### Enhancements
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This SDK provides a Browser & Node.js JavaScript client for calling [B2C Commerc

_For a Node.js only SDK that can also access Admin APIs checkout [Commerce SDK](https://github.com/SalesforceCommerceCloud/commerce-sdk)._

## :warning: Planned Shopper Context Changes :warning:

Starting July 31st 2024, all endpoints in the Shopper context API will require the `siteId` parameter for new customers. This field is marked as optional for backward compatibility and will be changed to mandatory tentatively by January 2025.

## Getting Started

### Requirements
Expand Down
27 changes: 27 additions & 0 deletions src/static/helpers/slasHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,40 @@ describe('Guest user flow', () => {
},
body: {
grant_type: 'client_credentials',
channel_id: 'site_id',
usid: 'usid',
},
};
expect(getAccessTokenMock).toBeCalledWith(expectedReqOptions);
expect(accessToken).toBe(expectedTokenResponse);
});

test('throws an error when channel_id is not passed into private client', async () => {
const mockSlasClient = createMockSlasClient();
const mockSlasClientNoSiteID = {
...mockSlasClient,
clientConfig: {
parameters: {
...mockSlasClient.clientConfig.parameters,
siteId: undefined, // siteId in client config is used for channel_id
},
},
};

await expect(
slasHelper.loginGuestUserPrivate(
// eslint-disable-next-line
// @ts-ignore
mockSlasClientNoSiteID,
parameters,
credentialsPrivate
)
).rejects.toThrow(
'Required argument channel_id is not provided through clientConfig.parameters.siteId'
);
});
});

describe('Registered B2C user flow', () => {
const expectedTokenBody = {
body: {
Expand Down
7 changes: 7 additions & 0 deletions src/static/helpers/slasHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ export async function loginGuestUserPrivate(
clientSecret: string;
}
): Promise<TokenResponse> {
if (!slasClient.clientConfig.parameters.siteId) {
throw new Error(
'Required argument channel_id is not provided through clientConfig.parameters.siteId'
);
}

const authorization = `Basic ${stringToBase64(
`${slasClient.clientConfig.parameters.clientId}:${credentials.clientSecret}`
)}`;
Expand All @@ -192,6 +198,7 @@ export async function loginGuestUserPrivate(
},
body: {
grant_type: 'client_credentials',
channel_id: slasClient.clientConfig.parameters.siteId,
...(parameters.usid && {usid: parameters.usid}),
},
};
Expand Down

0 comments on commit 1e893c7

Please sign in to comment.