diff --git a/modules/ceeIdSystem.js b/modules/ceeIdSystem.js index 5558e2c7d6fb..d1534ddada27 100644 --- a/modules/ceeIdSystem.js +++ b/modules/ceeIdSystem.js @@ -16,14 +16,13 @@ import {domainOverrideToRootDomain} from '../libraries/domainOverrideToRootDomai */ const MODULE_NAME = 'ceeId'; -const ID_TOKEN = 'WPxid'; export const storage = getStorageManager({ moduleName: MODULE_NAME, moduleType: MODULE_TYPE_UID }); /** * Reads the ID token from local storage or cookies. * @returns {string|undefined} The ID token, or undefined if not found. */ -export const readId = () => storage.getDataFromLocalStorage(ID_TOKEN) || storage.getCookie(ID_TOKEN); +export const readId = tokenName => storage.getDataFromLocalStorage(tokenName) || storage.getCookie(tokenName); /** @type {Submodule} */ export const ceeIdSubmodule = { @@ -44,9 +43,11 @@ export const ceeIdSubmodule = { * performs action to obtain id and return a value * @function * @returns {(IdResponse|undefined)} - */ - getId() { - const ceeIdToken = readId(); + */ + getId(config) { + const { params = {} } = config; + const { tokenName, value } = params + const ceeIdToken = value || readId(tokenName); return ceeIdToken ? { id: ceeIdToken } : undefined; }, diff --git a/modules/ceeIdSystem.md b/modules/ceeIdSystem.md index 811efe080694..fe7a543748dc 100644 --- a/modules/ceeIdSystem.md +++ b/modules/ceeIdSystem.md @@ -20,6 +20,10 @@ pbjs.setConfig({ name: 'ceeIdToken', expires: 7, refreshInSeconds: 360 + }, + params: { + tokenName: 'name' // Your custom name of token to read + value: 'tokenValue' // Optional param if you want to pass token value directly through setConfig (this param shouldn't be set if token value will be taken from cookie or LS) } }] } diff --git a/test/spec/modules/ceeIdSystem_spec.js b/test/spec/modules/ceeIdSystem_spec.js index 049e596ad151..62ae2898ede9 100644 --- a/test/spec/modules/ceeIdSystem_spec.js +++ b/test/spec/modules/ceeIdSystem_spec.js @@ -17,17 +17,65 @@ describe('ceeIdSystem', () => { }); describe('getId', () => { - it('should return an object with id when ceeIdToken is found', () => { + it('should return an object with id when ceeIdToken is found in LS', () => { + const config = { + name: 'ceeId', + storage: { + type: 'cookie', + name: 'ceeIdToken', + expires: 7, + refreshInSeconds: 360, + }, + params: { + tokenName: 'WPxid', + }, + }; + getDataFromLocalStorageStub.returns('testToken'); getCookieStub.returns('testToken'); - const result = ceeIdSubmodule.getId(); + const result = ceeIdSubmodule.getId(config); expect(result).to.deep.equal({ id: 'testToken' }); }); + it('should return an object with id when ceeIdToken is passed in setConfig', () => { + const config = { + name: 'ceeId', + storage: { + type: 'cookie', + name: 'ceeIdToken', + expires: 7, + refreshInSeconds: 360, + }, + params: { + tokenName: 'WPxid', + value: 'testTokenFromSetConfig' + }, + }; + + getDataFromLocalStorageStub.returns('testToken'); + getCookieStub.returns('testToken'); + + const result = ceeIdSubmodule.getId(config); + + expect(result).to.deep.equal({ id: 'testTokenFromSetConfig' }); + }); + it('should return undefined when ceeIdToken is not found', () => { - const result = ceeIdSubmodule.getId(); + const config = { + name: 'ceeId', + storage: { + type: 'cookie', + name: 'ceeIdToken', + expires: 7, + refreshInSeconds: 360, + }, + params: { + tokenName: 'WPxid', + }, + }; + const result = ceeIdSubmodule.getId(config); expect(result).to.be.undefined; }); @@ -49,27 +97,33 @@ describe('ceeIdSystem', () => { describe('readId', () => { it('should return data from local storage when it exists', () => { + const tokenName = 'testToken'; + getDataFromLocalStorageStub.returns('local_storage_data'); - const result = readId(); + const result = readId(tokenName); expect(result).to.equal('local_storage_data'); }); it('should return data from cookie when local storage data does not exist', () => { + const tokenName = 'testToken'; + getDataFromLocalStorageStub.returns(null); getCookieStub.returns('cookie_data'); - const result = readId(); + const result = readId(tokenName); expect(result).to.equal('cookie_data'); }); it('should return null when neither local storage data nor cookie data exists', () => { + const tokenName = 'testToken'; + getDataFromLocalStorageStub.returns(null); getCookieStub.returns(null); - const result = readId(); + const result = readId(tokenName); expect(result).to.be.null; });