Skip to content

Commit 09135d4

Browse files
authored
@W-14686284@ Fix Preview 403 errors when opening storefront being previewed in a new tab (#1629)
* Store SLAS refresh token in Local storage * Conditionally store SLAS refresh token * Add LocalAndCookie storage * Refactor LocalAndCookieStorage * Conditionally use Cookie key for Storefront Preview * PR Feedback
1 parent 36f965c commit 09135d4

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ jest.mock('commerce-sdk-isomorphic', () => {
3636

3737
jest.mock('../utils', () => ({
3838
__esModule: true,
39-
onClient: () => true
39+
onClient: () => true,
40+
getParentOrigin: jest.fn().mockResolvedValue(''),
41+
isOriginTrusted: () => false
4042
}))
4143

4244
/** The auth data we store has a slightly different shape than what we use. */

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {jwtDecode, JwtPayload} from 'jwt-decode'
1515
import {ApiClientConfigParams, Prettify, RemoveStringIndex} from '../hooks/types'
1616
import {BaseStorage, LocalStorage, CookieStorage, MemoryStorage, StorageType} from './storage'
1717
import {CustomerType} from '../hooks/useCustomerType'
18-
import {onClient} from '../utils'
18+
import {getParentOrigin, isOriginTrusted, onClient} from '../utils'
1919

2020
type TokenResponse = ShopperLoginTypes.TokenResponse
2121
type Helpers = typeof helpers
@@ -67,6 +67,8 @@ type AuthDataMap = Record<
6767
}
6868
>
6969

70+
const isParentTrusted = isOriginTrusted(getParentOrigin())
71+
7072
/**
7173
* A map of the data that this auth module stores. This maps the name of the property to
7274
* the storage type and the key when stored in that storage. You can also pass in a "callback"
@@ -107,16 +109,16 @@ const DATA_MAP: AuthDataMap = {
107109
},
108110
refresh_token_guest: {
109111
storageType: 'cookie',
110-
key: 'cc-nx-g',
112+
key: isParentTrusted ? 'cc-nx-g-iframe' : 'cc-nx-g',
111113
callback: (store) => {
112-
store.delete('cc-nx')
114+
store.delete(isParentTrusted ? 'cc-nx-iframe' : 'cc-nx')
113115
}
114116
},
115117
refresh_token_registered: {
116118
storageType: 'cookie',
117-
key: 'cc-nx',
119+
key: isParentTrusted ? 'cc-nx-iframe' : 'cc-nx',
118120
callback: (store) => {
119-
store.delete('cc-nx-g')
121+
store.delete(isParentTrusted ? 'cc-nx-g-iframe' : 'cc-nx-g')
120122
}
121123
},
122124
refresh_token_expires_in: {
@@ -129,16 +131,16 @@ const DATA_MAP: AuthDataMap = {
129131
// This triggers a new fetch for access_token using the current refresh_token from cookie storage and makes sure customer auth state is always in sync between SFRA and PWA sites in a hybrid setup.
130132
refresh_token_guest_copy: {
131133
storageType: 'local',
132-
key: 'cc-nx-g',
134+
key: isParentTrusted ? 'cc-nx-g-iframe' : 'cc-nx-g',
133135
callback: (store) => {
134-
store.delete('cc-nx')
136+
store.delete(isParentTrusted ? 'cc-nx-iframe' : 'cc-nx')
135137
}
136138
},
137139
refresh_token_registered_copy: {
138140
storageType: 'local',
139-
key: 'cc-nx',
141+
key: isParentTrusted ? 'cc-nx-iframe' : 'cc-nx',
140142
callback: (store) => {
141-
store.delete('cc-nx-g')
143+
store.delete(isParentTrusted ? 'cc-nx-g-iframe' : 'cc-nx-g')
142144
}
143145
},
144146
customer_type: {

0 commit comments

Comments
 (0)