Skip to content

Commit

Permalink
fix: post set_storage event when storage is cleared on logout
Browse files Browse the repository at this point in the history
.
  • Loading branch information
denysoblohin-okta committed May 5, 2022
1 parent 569994a commit fa79c0b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ export class TokenManager implements TokenManagerInterface {
}

// for synchronization of LocalStorage cross tabs for IE11
private emitSetStorageEvent() {
if (isIE11OrLess()) {
private emitSetStorageEvent(force = false) {
if (isIE11OrLess() || force) {
const storage = this.storage.getStorage();
this.emitter.emit(EVENT_SET_STORAGE, storage);
}
Expand Down Expand Up @@ -434,6 +434,7 @@ export class TokenManager implements TokenManagerInterface {
clear() {
this.clearExpireEventTimeoutAll();
this.storage.clearStorage();
this.emitSetStorageEvent(true);
}

clearPendingRemoveTokens() {
Expand Down
17 changes: 16 additions & 1 deletion test/spec/services/SyncStorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ describe('SyncStorageService', () => {
getStorage: jest.fn().mockImplementation(() => storage),
setStorage: jest.fn().mockImplementation((newStorage) => {
storage = newStorage;
})
}),
clearStorage: jest.fn().mockImplementation(() => {
storage = {};
}),
};
sdkMock = {
options: {},
Expand Down Expand Up @@ -210,6 +213,18 @@ describe('SyncStorageService', () => {
tokenManager.add('idToken', tokens.standardIdTokenParsed);
expect(serviceChannel.postMessage).toHaveBeenCalledTimes(1); // only "added"
});

it('should post "set_storage" event when token storage is cleared', () => {
createInstance();
const serviceChannel = (service as any).channel;
jest.spyOn(serviceChannel, 'postMessage');
tokenManager.clear();
expect(serviceChannel.postMessage).toHaveBeenCalledTimes(1);
expect(serviceChannel.postMessage).toHaveBeenNthCalledWith(1, {
type: 'set_storage',
storage: {},
});
});
});

describe('IE11', () => {
Expand Down

0 comments on commit fa79c0b

Please sign in to comment.