Skip to content

Commit e3bb633

Browse files
authored
@W-18348131: Fix inconsistency between dwsid and access token when hybrid authentication is enabled. (#2397)
* clear sfra access token before storing token response
1 parent f7a4c69 commit e3bb633

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

packages/commerce-sdk-react/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## v3.3.0-dev.1 (Apr 30, 2025)
2+
- Fix inconsistency between dwsid and access token for guest login when hybrid authentication is enabled [#2397](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2397)
3+
24
## v3.3.0-dev (Feb 18, 2025)
35
- Invalidate cache instead of removing cache when triggering logout [#2323](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2323)
46
- Fix dependencies vulnerabilities [#2338](https://github.com/SalesforceCommerceCloud/pwa-kit/pull/2338)

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,57 @@ describe('Auth', () => {
892892
await waitFor(() => {
893893
expect(auth.getDnt()).toBeUndefined()
894894
})
895+
getSpiedOn.mockRestore()
896+
parseSlasJWTSpiedOn.mockRestore()
897+
})
898+
899+
test('token call clears SFRA auth token cookie and sets all token from the response', async () => {
900+
const getDntSpy = jest.spyOn(Auth.prototype, 'getDnt')
901+
getDntSpy.mockImplementation((options?: {includeDefaults: boolean}) => {
902+
if (options?.includeDefaults) {
903+
return false
904+
}
905+
return undefined
906+
})
907+
const auth = new Auth(config)
908+
909+
// Set up initial SFRA auth token
910+
// @ts-expect-error private method
911+
auth.set('access_token_sfra', 'sfra_token')
912+
913+
// Verify the token was set correctly
914+
expect(auth.get('access_token_sfra')).toBe('sfra_token')
915+
916+
// Mock the token response that loginGuestUser will return
917+
const tokenResponse: ShopperLoginTypes.TokenResponse = {
918+
access_token:
919+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJjYy1zbGFzOjp6enJmXzAwMTo6c2NpZDpjOWM0NWJmZC0wZWQzLTRhYTIteHh4eC00MGY4ODk2MmI4MzY6OnVzaWQ6YjQ4NjUyMzMtZGU5Mi00MDM5LXh4eHgtYWEyZGZjOGMxZWE1IiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc2IiOiJ1aWRvOmVjb206OnVwbjpHdWVzdHx8am9obi5kb2VAZXhhbXBsZS5jb206OnVpZG46Sm9obiBEb2U6OmdjaWQ6Z3Vlc3QtMTIzNDU6OnJjaWQ6cmVnaXN0ZXJlZC02Nzg5MCIsImRudCI6InRlc3QifQ.9yKtUb22ExO-Q4VNQRAyIgTm63l3x5z45Uu1FIQa5dQ',
920+
customer_id: 'customer_id_xyz',
921+
enc_user_id: 'enc_user_id_xyz',
922+
expires_in: 1800,
923+
id_token: 'id_token_xyz',
924+
refresh_token: 'refresh_token_xyz',
925+
token_type: 'token_type_abc',
926+
usid: 'usid_xyz',
927+
idp_access_token: 'idp_access_token_xyz',
928+
refresh_token_expires_in: DEFAULT_SLAS_REFRESH_TOKEN_GUEST_TTL
929+
}
930+
931+
// Mock the helper to return token response
932+
const loginGuestUserSpy = jest.spyOn(helpers, 'loginGuestUser')
933+
loginGuestUserSpy.mockResolvedValueOnce(tokenResponse)
934+
935+
// Make the token call
936+
await auth.loginGuestUser()
937+
938+
// Verify SFRA auth token is cleared
939+
expect(auth.get('access_token_sfra')).toBeFalsy()
940+
941+
// Verify all token data is set correctly
942+
expect(auth.get('access_token')).toBe(tokenResponse.access_token)
943+
944+
// Clean up the spy
945+
getDntSpy.mockRestore()
895946
})
896947
})
897948

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ class Auth {
591591
* store the data in storage.
592592
*/
593593
private handleTokenResponse(res: TokenResponse, isGuest: boolean) {
594+
// Delete the SFRA auth token cookie if it exists
595+
this.clearSFRAAuthToken()
594596
this.set('access_token', res.access_token)
595597
this.set('customer_id', res.customer_id)
596598
this.set('enc_user_id', res.enc_user_id)

0 commit comments

Comments
 (0)