mkdir testSlasPrivateClient
cd testSlasPrivateClient
npm init -y
touch index.js
modify package.json in testSlasPrivateClient directory to point commerce-sdk-isomorphic to local directory in dependencies section and add "type": "module":
// demo client credentials, if you have access to your own please replace them below.
const CLIENT_ID = "";
const ORG_ID = "";
const SHORT_CODE = "";
const SITE_ID = "";
const CLIENT_SECRET = '' // SLAS PRIVATE SECRET HERE
// // EDIT HERE: Fill in with shopper credentials. Examples on how to register a shopper can be found in 04-register-shopper.ts
const shopper = {
username: ,
password: , // do not store password as plaintext. Store it in a secure location.
};
// must be registered in SLAS. On server, redirectURI is never called
const redirectURI = "http://localhost:3000/callback";
// using JWT to make SCAPI API for additional check that private slas is working as expected
const shopperSearch = new ShopperSearch({
...clientConfig,
headers: {authorization: Bearer ${guestTokenResponse.access_token}},
});
10. node index.js > out.txt
11. Observe out.txt and that the access_token is successfully retrieved with slas private secret and can be used to make SCAPI APIs
This PR adds support for SLAS private client without making a breaking change.
How to test
git clone git@github.com:SalesforceCommerceCloud/commerce-sdk-isomorphic.git
git checkout support-slas-client
// demo client credentials, if you have access to your own please replace them below. const CLIENT_ID = "";
const ORG_ID = "";
const SHORT_CODE = "";
const SITE_ID = "";
const CLIENT_SECRET = '' // SLAS PRIVATE SECRET HERE
// // EDIT HERE: Fill in with shopper credentials. Examples on how to register a shopper can be found in 04-register-shopper.ts
const shopper = {
username: ,
password: , // do not store password as plaintext. Store it in a secure location.
};
// must be registered in SLAS. On server, redirectURI is never called
const redirectURI = "http://localhost:3000/callback";
// client configuration parameters const clientConfig = { parameters: { clientId: CLIENT_ID, organizationId: ORG_ID, shortCode: SHORT_CODE, siteId: SITE_ID, }, };
const slasClient = new ShopperLogin(clientConfig);
// GUEST LOGIN const guestTokenResponse = await helpers .loginGuestUserPrivate(slasClient, { redirectURI }, {clientSecret: CLIENT_SECRET}) .then((guestTokenResponse) => { console.log("Guest Token Response: ", guestTokenResponse); return guestTokenResponse; }) .catch((error) => console.log("Error fetching token for guest login: ", error) ); // uncommnent the function you want to test // // REGISTERED B2C USER LOGIN // await helpers // .loginRegisteredUserB2C( // slasClient, // { username: shopper.username, password: shopper.password, clientSecret: CLIENT_SECRET }, // { redirectURI } // ) // .then((registeredUserTokenResponse) => { // console.log( // "Registered User Token Response: ", // registeredUserTokenResponse // ); // return registeredUserTokenResponse; // }) // .catch((error) => // console.log("Error fetching token for registered user login: ", error) // );
// REFRESH TOKEN // const refreshTokenResponse = await helpers // .refreshAccessToken(slasClient, { // refreshToken: guestTokenResponse.refresh_token, // }, {clientSecret: CLIENT_SECRET}) // .then((refreshTokenResponse) => { // console.log("Refresh Token Response: ", refreshTokenResponse); // return refreshTokenResponse; // }) // .catch((error) => console.log("Error refreshing token: ", error));
// using JWT to make SCAPI API for additional check that private slas is working as expected const shopperSearch = new ShopperSearch({ ...clientConfig, headers: {authorization:
Bearer ${guestTokenResponse.access_token}
}, });const searchResult = await shopperSearch.productSearch({ parameters: {q: 'shirt'}, }).then((result) => { console.log("RESULT", result) });
console.log(searchResult);