Skip to content

Commit

Permalink
Conditionally use Cookie key for Storefront Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraya committed Jan 3, 2024
1 parent 627dc0f commit 3d4d9c7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 89 deletions.
9 changes: 6 additions & 3 deletions packages/commerce-sdk-react/src/auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ jest.mock('./storage', () => {
return {
...originalModule,
CookieStorage: originalModule.MemoryStorage,
LocalStorage: originalModule.MemoryStorage,
LocalAndCookieStorage: originalModule.MemoryStorage
LocalStorage: originalModule.MemoryStorage
}
})

Expand All @@ -37,7 +36,9 @@ jest.mock('commerce-sdk-isomorphic', () => {

jest.mock('../utils', () => ({
__esModule: true,
onClient: () => true
onClient: () => true,
getParentOrigin: jest.fn().mockResolvedValue(''),
isOriginTrusted: () => false
}))

/** The auth data we store has a slightly different shape than what we use. */
Expand Down Expand Up @@ -68,6 +69,8 @@ describe('Auth', () => {
expect(auth.get('refresh_token_guest')).toBe(refreshToken)
expect(auth.get('access_token')).toBe(accessToken)
// @ts-expect-error private property
expect([...auth.stores['cookie'].map.keys()]).toEqual([`cc-nx-g_siteId`])
// @ts-expect-error private property
expect([...auth.stores['local'].map.keys()]).toEqual([`access_token_siteId`])
})
test('set registered refresh token will clear guest refresh token, vise versa', () => {
Expand Down
36 changes: 14 additions & 22 deletions packages/commerce-sdk-react/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ import {
} from 'commerce-sdk-isomorphic'
import {jwtDecode, JwtPayload} from 'jwt-decode'
import {ApiClientConfigParams, Prettify, RemoveStringIndex} from '../hooks/types'
import {
BaseStorage,
LocalStorage,
CookieStorage,
MemoryStorage,
StorageType,
LocalAndCookieStorage
} from './storage'
import {BaseStorage, LocalStorage, CookieStorage, MemoryStorage, StorageType} from './storage'
import {CustomerType} from '../hooks/useCustomerType'
import {onClient} from '../utils'
import {getParentOrigin, isOriginTrusted, onClient} from '../utils'

type TokenResponse = ShopperLoginTypes.TokenResponse
type Helpers = typeof helpers
Expand Down Expand Up @@ -74,6 +67,8 @@ type AuthDataMap = Record<
}
>

const isStorefrontPreview = isOriginTrusted(getParentOrigin())

/**
* A map of the data that this auth module stores. This maps the name of the property to
* the storage type and the key when stored in that storage. You can also pass in a "callback"
Expand Down Expand Up @@ -113,17 +108,17 @@ const DATA_MAP: AuthDataMap = {
key: 'token_type'
},
refresh_token_guest: {
storageType: 'localandcookie',
key: 'cc-nx-g',
storageType: 'cookie',
key: isStorefrontPreview ? 'cc-nx-g-preview' : 'cc-nx-g',
callback: (store) => {
store.delete('cc-nx')
store.delete(isStorefrontPreview ? 'cc-nx-preview' : 'cc-nx')
}
},
refresh_token_registered: {
storageType: 'localandcookie',
key: 'cc-nx',
storageType: 'cookie',
key: isStorefrontPreview ? 'cc-nx-preview' : 'cc-nx',
callback: (store) => {
store.delete('cc-nx-g')
store.delete(isStorefrontPreview ? 'cc-nx-g-preview' : 'cc-nx-g')
}
},
refresh_token_expires_in: {
Expand All @@ -136,16 +131,16 @@ const DATA_MAP: AuthDataMap = {
// 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.
refresh_token_guest_copy: {
storageType: 'local',
key: 'cc-nx-g',
key: isStorefrontPreview ? 'cc-nx-g-preview' : 'cc-nx-g',
callback: (store) => {
store.delete('cc-nx')
store.delete(isStorefrontPreview ? 'cc-nx-preview' : 'cc-nx')
}
},
refresh_token_registered_copy: {
storageType: 'local',
key: 'cc-nx',
key: isStorefrontPreview ? 'cc-nx-preview' : 'cc-nx',
callback: (store) => {
store.delete('cc-nx-g')
store.delete(isStorefrontPreview ? 'cc-nx-g-preview' : 'cc-nx-g')
}
},
customer_type: {
Expand Down Expand Up @@ -206,9 +201,6 @@ class Auth {
this.stores = {
cookie: onClient() ? new CookieStorage(options) : new MemoryStorage(options),
local: onClient() ? new LocalStorage(options) : new MemoryStorage(options),
localandcookie: onClient()
? new LocalAndCookieStorage(options)
: new MemoryStorage(options),
memory: new MemoryStorage(options)
}

Expand Down
3 changes: 1 addition & 2 deletions packages/commerce-sdk-react/src/auth/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export type StorageType = 'cookie' | 'local' | 'localandcookie' | 'memory'
export type StorageType = 'cookie' | 'local' | 'memory'
export * from './base'
export * from './cookie'
export * from './local'
export * from './localandcookie'
export * from './memory'
62 changes: 0 additions & 62 deletions packages/commerce-sdk-react/src/auth/storage/localandcookie.ts

This file was deleted.

0 comments on commit 3d4d9c7

Please sign in to comment.