From 3ebf7c73562995d6bcec1121b973db899c9f509b Mon Sep 17 00:00:00 2001 From: Ujwala <59447810+unandyala@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:49:55 -0500 Subject: [PATCH] Allow cookies in shopper login API (#2190) * Allow cookies in shopper login API --- packages/commerce-sdk-react/CHANGELOG.md | 1 + .../commerce-sdk-react/src/auth/index.test.ts | 88 ++++++++++++++++++- packages/commerce-sdk-react/src/auth/index.ts | 8 +- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/packages/commerce-sdk-react/CHANGELOG.md b/packages/commerce-sdk-react/CHANGELOG.md index 03c0163b9e..76875a2cee 100644 --- a/packages/commerce-sdk-react/CHANGELOG.md +++ b/packages/commerce-sdk-react/CHANGELOG.md @@ -1,4 +1,5 @@ ## v3.2.0-dev (Oct 29, 2024) +- Allow cookies for ShopperLogin API [#2190](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2190 - Fix refresh token TTL warning from firing when override is not provided [#2114](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2114) - Update CacheUpdateMatrix for mergeBasket mutation [#2138](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2092) diff --git a/packages/commerce-sdk-react/src/auth/index.test.ts b/packages/commerce-sdk-react/src/auth/index.test.ts index 915730f3d2..21e198ab15 100644 --- a/packages/commerce-sdk-react/src/auth/index.test.ts +++ b/packages/commerce-sdk-react/src/auth/index.test.ts @@ -7,7 +7,7 @@ import Auth, {AuthData} from './' import {waitFor} from '@testing-library/react' import jwt from 'jsonwebtoken' -import {helpers, ShopperCustomersTypes} from 'commerce-sdk-isomorphic' +import {helpers, ShopperCustomersTypes, ShopperLogin} from 'commerce-sdk-isomorphic' import * as utils from '../utils' import {SLAS_SECRET_PLACEHOLDER} from '../constant' import {ShopperLoginTypes} from 'commerce-sdk-isomorphic' @@ -15,7 +15,7 @@ import { DEFAULT_SLAS_REFRESH_TOKEN_REGISTERED_TTL, DEFAULT_SLAS_REFRESH_TOKEN_GUEST_TTL } from './index' -import {RequireKeys} from '../hooks/types' +import {ApiClientConfigParams, RequireKeys} from '../hooks/types' const baseCustomer: RequireKeys = { customerId: 'customerId', @@ -720,3 +720,87 @@ describe('Auth', () => { }) }) }) + +describe('Auth service sends credentials fetch option to the ShopperLogin API', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + test('Adds fetch options with credentials when not defined in config', async () => { + const auth = new Auth(config) + await auth.loginGuestUser() + + // Ensure the helper method was called + expect(helpers.loginGuestUser).toHaveBeenCalled() + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) + + // Check that the correct parameters were passed to the helper + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] + expect(callArguments).toBeDefined() + expect(callArguments.length).toBeGreaterThan(0) + + const shopperLogin: ShopperLogin = callArguments[0] + expect(shopperLogin).toBeDefined() + expect(shopperLogin.clientConfig).toBeDefined() + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() + + // Ensure fetch options include the expected credentials + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('same-origin') + }) + + test('Does not override the credentials in fetch options if already exists', async () => { + const configWithFetchOptions = { + ...config, + fetchOptions: { + credentials: 'include' + } + } + const auth = new Auth(configWithFetchOptions) + await auth.loginGuestUser() + + // Ensure the helper method was called + expect(helpers.loginGuestUser).toHaveBeenCalled() + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) + + // Check that the correct parameters were passed to the helper + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] + expect(callArguments).toBeDefined() + expect(callArguments.length).toBeGreaterThan(0) + + const shopperLogin: ShopperLogin = callArguments[0] + expect(shopperLogin).toBeDefined() + expect(shopperLogin.clientConfig).toBeDefined() + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() + + // Ensure fetch options include the expected credentials + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('include') + }) + + test('Adds credentials to the fetch options if it is missing', async () => { + const configWithFetchOptions = { + ...config, + fetchOptions: { + cache: 'no-cache' + } + } + const auth = new Auth(configWithFetchOptions) + await auth.loginGuestUser() + + // Ensure the helper method was called + expect(helpers.loginGuestUser).toHaveBeenCalled() + expect(helpers.loginGuestUser).toHaveBeenCalledTimes(1) + + // Check that the correct parameters were passed to the helper + const callArguments = (helpers.loginGuestUser as jest.Mock).mock.calls[0] + expect(callArguments).toBeDefined() + expect(callArguments.length).toBeGreaterThan(0) + + const shopperLogin: ShopperLogin = callArguments[0] + expect(shopperLogin).toBeDefined() + expect(shopperLogin.clientConfig).toBeDefined() + expect(shopperLogin.clientConfig.fetchOptions).toBeDefined() + + // Ensure fetch options include the expected credentials + expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('same-origin') + }) +}) diff --git a/packages/commerce-sdk-react/src/auth/index.ts b/packages/commerce-sdk-react/src/auth/index.ts index 59db0fbb44..916b4ecab1 100644 --- a/packages/commerce-sdk-react/src/auth/index.ts +++ b/packages/commerce-sdk-react/src/auth/index.ts @@ -218,7 +218,13 @@ class Auth { siteId: config.siteId }, throwOnBadResponse: true, - fetchOptions: config.fetchOptions + // We need to set credentials to 'same-origin' to allow cookies to be set. + // This is required as SLAS calls return a dwsid cookie for hybrid sites. + // The dwsid value is then passed to the SCAPI as a header maintain the server affinity. + fetchOptions: { + credentials: 'same-origin', + ...config.fetchOptions + } }) this.shopperCustomersClient = new ShopperCustomers({ proxy: config.proxy,