Skip to content

Commit

Permalink
Namespace cookies with siteId
Browse files Browse the repository at this point in the history
  • Loading branch information
shethj committed Oct 3, 2024
1 parent 1d38638 commit 81ba02f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/template-retail-react-app/app/commerce-api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class Auth {
this._config = api._config
this._onClient = typeof window !== 'undefined'

this._cookieStorage = this._onClient ? new CookieStorage() : new Map()
const _options = {
keySuffix: this._config.parameters.siteId,
}

this._cookieStorage = this._onClient ? new CookieStorage(_options) : new Map()
this._storage = this._onClient ? new LocalStorage() : new Map()

const configOid = api._config.parameters.organizationId
Expand Down Expand Up @@ -109,11 +113,11 @@ class Auth {
}

get usid() {
return this._storage.get(usidStorageKey)
return this._cookieStorage.get(usidStorageKey)
}

set usid(usid) {
this._storage.set(usidStorageKey, usid)
this._cookieStorage.set(usidStorageKey, usid)
}

get cid() {
Expand Down Expand Up @@ -485,6 +489,7 @@ export default Auth

class Storage {
constructor(options = {}) {
this.options = options
if (typeof this.options.keySuffix !== 'string') this.options.keySuffix = ''
}
getSuffixedKey(key) {
Expand All @@ -497,7 +502,8 @@ class Storage {

class CookieStorage extends Storage {
constructor(...args) {
super(args)
// Accessing 0 index considering options{} is the first argument being passed to the constructor extending this class
super(args[0])
if (typeof document === 'undefined') {
throw new Error('CookieStorage is not avaliable on the current environment.')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,6 @@ export function hasSFRAAuthStateChanged(storage, cookieStorage) {

// If a cc-at cookie is found and it's value is NOT 'refresh',
// Update localStoreage token cookie to cc-at value and delete cc-at cookie.
storage.set(tokenStorageKey, SFRAAuthToken)
storage.set(tokenStorageKey, `Bearer ${SFRAAuthToken}`)
cookieStorage.delete(ECOM_ACCESS_TOKEN_STORAGE_KEY)
}

0 comments on commit 81ba02f

Please sign in to comment.