Skip to content

Commit 1611a5b

Browse files
authored
Merge pull request #3151 from SalesforceCommerceCloud/hotfix/skip-deleting-ecom-session-hybrid-auth
@W-19391396: [Bugfix] Do not delete DWSID on shopper login if hybrid auth is enabled
2 parents 6512d91 + c487d6c commit 1611a5b

File tree

9 files changed

+85
-3
lines changed

9 files changed

+85
-3
lines changed

packages/commerce-sdk-react/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Update USID expiry to match SLAS refresh token expiry[#2854](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2854)
44

5+
- [Bugfix] Skip deleting dwsid on shopper login if hybrid auth is enabled for current site. [#3151](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3151)
6+
57
## v3.4.0 (Jul 22, 2025)
68

79
- Optionally disable auth init in CommerceApiProvider [#2629](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2629)

packages/commerce-sdk-react/src/auth/index.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,3 +1212,45 @@ describe('Auth service sends credentials fetch option to the ShopperLogin API',
12121212
expect(shopperLogin.clientConfig.fetchOptions.credentials).toBe('same-origin')
12131213
})
12141214
})
1215+
1216+
describe('hybridAuthEnabled property toggles clearECOMSession', () => {
1217+
beforeEach(() => {
1218+
jest.clearAllMocks()
1219+
})
1220+
1221+
test('clears DWSID cookie when hybridAuthEnabled is false', () => {
1222+
const auth = new Auth({...config, hybridAuthEnabled: false})
1223+
1224+
// Set a DWSID cookie value
1225+
// @ts-expect-error private method
1226+
auth.set('dwsid', 'test-dwsid-value')
1227+
1228+
// Verify the cookie was set
1229+
expect(auth.get('dwsid')).toBe('test-dwsid-value')
1230+
1231+
// Call clearECOMSession
1232+
// @ts-expect-error private method
1233+
auth.clearECOMSession()
1234+
1235+
// Verify the cookie was cleared
1236+
expect(auth.get('dwsid')).toBeFalsy()
1237+
})
1238+
1239+
test('does NOT clear DWSID cookie when hybridAuthEnabled is true', () => {
1240+
const auth = new Auth({...config, hybridAuthEnabled: true})
1241+
1242+
// Set a DWSID cookie value
1243+
// @ts-expect-error private method
1244+
auth.set('dwsid', 'test-dwsid-value')
1245+
1246+
// Verify the cookie was set
1247+
expect(auth.get('dwsid')).toBe('test-dwsid-value')
1248+
1249+
// Call clearECOMSession
1250+
// @ts-expect-error private method
1251+
auth.clearECOMSession()
1252+
1253+
// Verify the cookie was NOT cleared
1254+
expect(auth.get('dwsid')).toBe('test-dwsid-value')
1255+
})
1256+
})

packages/commerce-sdk-react/src/auth/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ interface AuthConfig extends ApiClientConfigParams {
5353
passwordlessLoginCallbackURI?: string
5454
refreshTokenRegisteredCookieTTL?: number
5555
refreshTokenGuestCookieTTL?: number
56+
hybridAuthEnabled?: boolean
5657
}
5758

5859
interface JWTHeaders {
@@ -243,6 +244,8 @@ class Auth {
243244
| ((loginId: string, usid: string, refresh: boolean) => Promise<TokenResponse>)
244245
| undefined
245246

247+
private hybridAuthEnabled: boolean
248+
246249
constructor(config: AuthConfig) {
247250
// Special endpoint for injecting SLAS private client secret.
248251
const baseUrl = config.proxy.split(MOBIFY_PATH)[0]
@@ -339,6 +342,8 @@ class Auth {
339342
? passwordlessLoginCallbackURI
340343
: `${baseUrl}${passwordlessLoginCallbackURI}`
341344
: ''
345+
346+
this.hybridAuthEnabled = config.hybridAuthEnabled || false
342347
}
343348

344349
get(name: AuthDataKeys) {
@@ -576,6 +581,14 @@ class Auth {
576581
* registered shopper refresh-token and restores session and basket on SFRA.
577582
*/
578583
private clearECOMSession() {
584+
/**
585+
* If `hybridAuthEnabled` is true, dwsid cookie must not be cleared.
586+
* This makes sure the session-bridged dwsid, received from `/oauth2/token` call on shopper login
587+
* is NOT cleared and can be used to maintain the server affinity.
588+
*/
589+
if (this.hybridAuthEnabled) {
590+
return
591+
}
579592
const {key, storageType} = DATA_MAP[DWSID_COOKIE_NAME]
580593
const store = this.stores[storageType]
581594
store.delete(key)

packages/commerce-sdk-react/src/provider.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface CommerceApiProviderProps extends ApiClientConfigParams {
5050
refreshTokenGuestCookieTTL?: number
5151
apiClients?: ApiClients
5252
disableAuthInit?: boolean
53+
hybridAuthEnabled?: boolean
5354
}
5455

5556
/**
@@ -108,6 +109,17 @@ export const AuthContext = React.createContext({} as Auth)
108109
* Non-PWA Kit users can enable private client mode by passing in a client secret
109110
* directly to the provider. However, be careful when doing this as you will have
110111
* to make sure the secret is not unexpectedly exposed to the client.
112+
*
113+
*
114+
* `hybridAuthEnabled` is an optional flag that indicates the current Site has Hybrid Auth enabled.
115+
* This drives the behavior of the `clearECOMSession` method. If `hybridAuthEnabled` is true,
116+
* the `clearECOMSession` method will not be called. This makes sure the session-bridged dwsid, received from `/oauth2/token` call
117+
* on shopper login is NOT cleared and can be used to maintain the server affinity.
118+
*
119+
* `hybridAuthEnabled` flag can also be used to drive other Hybrid Auth specific behaviors in the future.
120+
*
121+
* Note: `hybridAuthEnabled` should NOT be set to true for hybrid storefronts using Plugin SLAS as we need the dwsid to be deleted
122+
* to force session-bridging on SFRA as in this case, the `oauth2/token` call does not return a dwsid.
111123
*
112124
* @returns Provider to wrap your app with
113125
*/
@@ -134,7 +146,8 @@ const CommerceApiProvider = (props: CommerceApiProviderProps): ReactElement => {
134146
refreshTokenRegisteredCookieTTL,
135147
refreshTokenGuestCookieTTL,
136148
apiClients,
137-
disableAuthInit = false
149+
disableAuthInit = false,
150+
hybridAuthEnabled = false
138151
} = props
139152

140153
// Set the logger based on provided configuration, or default to the console object if no logger is provided
@@ -157,7 +170,8 @@ const CommerceApiProvider = (props: CommerceApiProviderProps): ReactElement => {
157170
defaultDnt,
158171
passwordlessLoginCallbackURI,
159172
refreshTokenRegisteredCookieTTL,
160-
refreshTokenGuestCookieTTL
173+
refreshTokenGuestCookieTTL,
174+
hybridAuthEnabled
161175
})
162176
}, [
163177
clientId,
@@ -176,7 +190,8 @@ const CommerceApiProvider = (props: CommerceApiProviderProps): ReactElement => {
176190
passwordlessLoginCallbackURI,
177191
refreshTokenRegisteredCookieTTL,
178192
refreshTokenGuestCookieTTL,
179-
apiClients
193+
apiClients,
194+
hybridAuthEnabled
180195
])
181196

182197
const dwsid = auth.get(DWSID_COOKIE_NAME)

packages/pwa-kit-create-app/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## v3.12.0-dev (Jul 28, 2025)
22
- This feature introduces an AI-powered shopping assistant that integrates Salesforce Embedded Messaging Service with PWA Kit applications. The shopper agent provides real-time chat support, search assistance, and personalized shopping guidance directly within the e-commerce experience. [#2658](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2658)
33

4+
- Update _app-config generator template to include `hybridAuthEnabled` prop [#3151](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3151)
5+
46
## v3.11.0 (Jul 22, 2025)
57
- Fix the demo instance details in `program.json`[#2800](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2800)
68
- Fix exiting before `program.json` content can be flushed [#2699](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2699)

packages/pwa-kit-create-app/assets/bootstrap/js/overrides/app/components/_app-config/index.jsx.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const AppConfig = ({children, locals = {}}) => {
8787
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
8888
enablePWAKitPrivateClient={true}
8989
{{/if}}
90+
// Uncomment 'hybridAuthEnabled' if the current site has Hybrid Auth enabled. Do NOT set this flag for hybrid storefronts using Plugin SLAS.
91+
// hybridAuthEnabled={true}
9092
>
9193
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>
9294
<StoreLocatorProvider config={storeLocatorConfig}>

packages/pwa-kit-create-app/assets/templates/@salesforce/retail-react-app/app/components/_app-config/index.jsx.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const AppConfig = ({children, locals = {}}) => {
8787
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
8888
enablePWAKitPrivateClient={true}
8989
{{/if}}
90+
// Uncomment 'hybridAuthEnabled' if the current site has Hybrid Auth enabled. Do NOT set this flag for hybrid storefronts using Plugin SLAS.
91+
// hybridAuthEnabled={true}
9092
>
9193
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>
9294
<StoreLocatorProvider config={storeLocatorConfig}>

packages/template-retail-react-app/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- [Bugfix] Pin `@chakra-ui/react` version to 2.7.0 to avoid breaking changes from 2.10.9 [#2658](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2658)
88

9+
- Introduce optional prop `hybridAuthEnabled` to control Hybrid Auth specific behaviors in commerce-sdk-react [#3151](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/3151)
10+
911
## v7.0.0 (July 22, 2025)
1012

1113
- Improved the layout of product tiles in product scroll and product list [#2446](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2446)

packages/template-retail-react-app/app/components/_app-config/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ const AppConfig = ({children, locals = {}}) => {
9292
// Uncomment 'enablePWAKitPrivateClient' to use SLAS private client login flows.
9393
// Make sure to also enable useSLASPrivateClient in ssr.js when enabling this setting.
9494
// enablePWAKitPrivateClient={true}
95+
// Uncomment 'hybridAuthEnabled' if the current site has Hybrid Auth enabled. Do NOT set this flag for hybrid storefronts using Plugin SLAS.
96+
// hybridAuthEnabled={true}
9597
logger={createLogger({packageName: 'commerce-sdk-react'})}
9698
>
9799
<MultiSiteProvider site={locals.site} locale={locals.locale} buildUrl={locals.buildUrl}>

0 commit comments

Comments
 (0)