diff --git a/__tests__/transaction-manager.test.ts b/__tests__/transaction-manager.test.ts index d5841c5b5..e0846f69f 100644 --- a/__tests__/transaction-manager.test.ts +++ b/__tests__/transaction-manager.test.ts @@ -1,5 +1,5 @@ import { TransactionManager } from '../src/transaction-manager'; -import { SessionStorage } from '../src/storage'; +import { CookieStorage, SessionStorage } from '../src/storage'; import { TEST_CLIENT_ID, TEST_STATE } from './constants'; import { expect } from '@jest/globals'; @@ -81,4 +81,36 @@ describe('transaction manager', () => { expect(sessionStorage.removeItem).toHaveBeenCalledWith(transactionKey()); }); }); + + describe('CookieStorage usage', () => { + it("`create` saves the transaction in the storage with the provided domain", () => { + CookieStorage.save = jest.fn(); + const cookieDomain = "vanity.auth.com"; + tm = new TransactionManager(CookieStorage, TEST_CLIENT_ID, cookieDomain); + tm.create(transaction); + + expect(CookieStorage.save).toHaveBeenCalledWith( + transactionKey(), + expect.anything(), + { + daysUntilExpire: 1, + cookieDomain: cookieDomain + } + ); + }); + + it("`remove` deletes the transaction in the storage with the provided domain", () => { + CookieStorage.remove = jest.fn(); + const cookieDomain = "vanity.auth.com"; + tm = new TransactionManager(CookieStorage, TEST_CLIENT_ID, cookieDomain); + tm.remove(); + + expect(CookieStorage.remove).toHaveBeenCalledWith( + transactionKey(), + { + cookieDomain: cookieDomain + } + ); + }); + }); }); diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index c2ee187cb..78d658af9 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -207,7 +207,8 @@ export class Auth0Client { this.transactionManager = new TransactionManager( transactionStorage, - this.options.clientId + this.options.clientId, + this.options.cookieDomain, ); this.nowProvider = this.options.nowProvider || DEFAULT_NOW_PROVIDER; diff --git a/src/transaction-manager.ts b/src/transaction-manager.ts index b2f7fa4de..17a22074c 100644 --- a/src/transaction-manager.ts +++ b/src/transaction-manager.ts @@ -17,7 +17,7 @@ export class TransactionManager { private transaction: Transaction | undefined; private storageKey: string; - constructor(private storage: ClientStorage, private clientId: string) { + constructor(private storage: ClientStorage, private clientId: string, private cookieDomain?: string) { this.storageKey = `${TRANSACTION_STORAGE_KEY_PREFIX}.${this.clientId}`; this.transaction = this.storage.get(this.storageKey); } @@ -26,7 +26,8 @@ export class TransactionManager { this.transaction = transaction; this.storage.save(this.storageKey, transaction, { - daysUntilExpire: 1 + daysUntilExpire: 1, + cookieDomain: this.cookieDomain, }); } @@ -36,6 +37,8 @@ export class TransactionManager { public remove() { delete this.transaction; - this.storage.remove(this.storageKey); + this.storage.remove(this.storageKey, { + cookieDomain: this.cookieDomain + }); } }