Skip to content

Commit

Permalink
Make TransactionManager use CookieDomain (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZdravkoDonev-gtmhub committed Jun 2, 2023
1 parent 332f21e commit f7594e5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
34 changes: 33 additions & 1 deletion __tests__/transaction-manager.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
}
);
});
});
});
3 changes: 2 additions & 1 deletion src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions src/transaction-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -26,7 +26,8 @@ export class TransactionManager {
this.transaction = transaction;

this.storage.save(this.storageKey, transaction, {
daysUntilExpire: 1
daysUntilExpire: 1,
cookieDomain: this.cookieDomain,
});
}

Expand All @@ -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
});
}
}

0 comments on commit f7594e5

Please sign in to comment.