Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@W-14758586: Support slas client #140

Merged
merged 7 commits into from
Jan 26, 2024
Merged

@W-14758586: Support slas client #140

merged 7 commits into from
Jan 26, 2024

Conversation

alexvuong
Copy link
Contributor

@alexvuong alexvuong commented Jan 20, 2024

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

How to test

  1. git clone [email protected]: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
  1. 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>"
  }
  1. npm install
  2. 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
  3. 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 = "<CLIENT_ID>";
const ORG_ID = "<ORG_ID>";
const SHORT_CODE = "<SHORT_CODE>";
const SITE_ID = "<SITE_ID>";
const CLIENT_SECRET = '<SLAS PRIVATE SECRET HERE>' // 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: <username>,
   password: <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);
  1. node index.js > out.txt
  2. Observe out.txt and that the access_token is successfully retrieved with slas private secret and can be used to make SCAPI APIs

@alexvuong alexvuong changed the title @W-14758586: Support slas client @W-14758586: Support slas client (WIP) Jan 20, 2024
@alexvuong alexvuong marked this pull request as ready for review January 22, 2024 22:14
@alexvuong alexvuong requested a review from a team as a code owner January 22, 2024 22:14
@alexvuong alexvuong self-assigned this Jan 22, 2024
@alexvuong alexvuong changed the title @W-14758586: Support slas client (WIP) @W-14758586: Support slas client Jan 23, 2024
src/static/helpers/slasHelper.ts Show resolved Hide resolved
src/static/helpers/slasHelper.ts Outdated Show resolved Hide resolved
src/static/helpers/slasHelper.test.ts Outdated Show resolved Hide resolved
joeluong-sfcc
joeluong-sfcc previously approved these changes Jan 24, 2024
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@alexvuong alexvuong requested a review from vcua-mobify January 24, 2024 22:22
joeluong-sfcc
joeluong-sfcc previously approved these changes Jan 25, 2024
Copy link
Contributor

@vcua-mobify vcua-mobify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with the test script and everything seems to be good to go.

One thing I am curious about the warning. Since it always appears, is there a possibility of the warning appearing on a production site and an end shopper seeing the warning?

joeluong-sfcc
joeluong-sfcc previously approved these changes Jan 26, 2024
Copy link
Contributor

@vcua-mobify vcua-mobify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into the warning @alexvuong and for applying the feedback.

@alexvuong alexvuong merged commit 2702336 into main Jan 26, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants