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 b4d565c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,14 @@ export class TokenManager implements TokenManagerInterface {
}

clear() {
const tokens = this.getTokensSync();
this.clearExpireEventTimeoutAll();
this.storage.clearStorage();
this.emitSetStorageEvent();

Object.keys(tokens).forEach(key => {
this.emitRemoved(key, tokens[key]);
});
}

clearPendingRemoveTokens() {
Expand Down
38 changes: 36 additions & 2 deletions 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 @@ -140,7 +143,7 @@ describe('SyncStorageService', () => {
expect(sdkMock.emitter.emit).toHaveBeenCalledWith('renewed', 'idToken', tokens.standardIdToken2Parsed, tokens.standardIdTokenParsed);
});

it('should not post "sync message" to other tabs', async () => {
it('should not post sync message to other tabs', async () => {
createInstance();
const serviceChannel = (service as any).channel;
jest.spyOn(serviceChannel, 'postMessage');
Expand Down Expand Up @@ -203,6 +206,19 @@ describe('SyncStorageService', () => {
});
});

it('should post "remove" events 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: 'removed',
key: 'idToken',
token: tokens.standardIdTokenParsed
});
});

it('should not post "set_storage" event on storage change (for non-IE)', () => {
createInstance();
const serviceChannel = (service as any).channel;
Expand Down Expand Up @@ -254,6 +270,24 @@ describe('SyncStorageService', () => {
});
});

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(2);
expect(serviceChannel.postMessage).toHaveBeenNthCalledWith(1, {
type: 'set_storage',
storage: {
},
});
expect(serviceChannel.postMessage).toHaveBeenNthCalledWith(2, {
type: 'removed',
key: 'idToken',
token: tokens.standardIdTokenParsed
});
});

it('should post "set_storage" event when token is chnaged', () => {
createInstance();
const serviceChannel = (service as any).channel;
Expand Down

0 comments on commit b4d565c

Please sign in to comment.