SalesforceCommerceCloud / commerce-sdk-isomorphic

Browser & Node.js JavaScript client for B2C Commerce API
https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/
BSD 3-Clause "New" or "Revised" License
42 stars 20 forks source link

@W-14758586: Support slas client #140

Closed alexvuong closed 7 months ago

alexvuong commented 8 months ago

This PR adds support for SLAS private client without making a breaking change.

How to test

  1. git clone git@github.com:SalesforceCommerceCloud/commerce-sdk-isomorphic.git
  2. git checkout support-slas-client
  3. yarn install
  4. npm run renderTemplates && yarn build:lib
  5. Create a node project in a separate directory
    mkdir testSlasPrivateClient
    cd testSlasPrivateClient
    npm init -y
    touch index.js
  6. modify package.json in testSlasPrivateClient directory to point commerce-sdk-isomorphic to local directory in dependencies section and add "type": "module":
    "type": "module",
    "dependencies": {
    "commerce-sdk-isomorphic": "<insert_path_to_local_commerce-sdk-isomorphic>"
    }
  7. npm install
  8. Set up SLAS private client via API for the instance that you are testing on https://kv7kzm78.api.commercecloud.salesforce.com/shopper/auth-admin/v1/sso/login
  9. Add test code to index.js and replace client creds
    
    import pkg from 'commerce-sdk-isomorphic';
    const { helpers, ShopperLogin, ShopperSearch } = pkg;

// 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);


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