Skip to content

Commit 0133d83

Browse files
fix: fix unit tests and add check if keyValueStore has guest token
1 parent 2d9d94e commit 0133d83

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

packages/auth/__tests__/providers/cognito/credentialsProvider/IdentityIdStore.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('DefaultIdentityIdStore', () => {
4747

4848
afterEach(() => {
4949
mockKeyValueStorage.setItem.mockClear();
50-
mockKeyValueStorage.getItem.mockClear();
50+
mockKeyValueStorage.getItem.mockReset();
5151
mockKeyValueStorage.removeItem.mockClear();
5252
mockKeyValueStorage.clear.mockClear();
5353
});
@@ -73,6 +73,7 @@ describe('DefaultIdentityIdStore', () => {
7373
});
7474

7575
it('should store primary identityId in keyValueStorage', async () => {
76+
mockKeyValueStorage.getItem.mockResolvedValue(validGuestIdentityId.id);
7677
defaultIdStore.storeIdentityId(validPrimaryIdentityId);
7778
expect(mockKeyValueStorage.removeItem).toHaveBeenCalledWith(
7879
validAuthKey.identityId,
@@ -114,6 +115,7 @@ describe('DefaultIdentityIdStore', () => {
114115
});
115116

116117
it('should not call keyValueStorage.removeItem when there is no guest identityId to clear', async () => {
118+
mockKeyValueStorage.getItem.mockReturnValue(null);
117119
const refreshIdentityIdStore = new DefaultIdentityIdStore(
118120
mockKeyValueStorage,
119121
);

packages/auth/src/providers/cognito/credentialsProvider/IdentityIdStore.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ export class DefaultIdentityIdStore implements IdentityIdStore {
8383
// On the server-side we use the _hasGuestIdentityId flag to avoid caching issues
8484
const serverside = typeof window === 'undefined';
8585
if (this._hasGuestIdentityId || !serverside) {
86-
this.keyValueStorage.removeItem(this._authKeys.identityId);
86+
if (this.keyValueStorage.getItem(this._authKeys.identityId) !== null) {
87+
this.keyValueStorage.removeItem(this._authKeys.identityId);
88+
}
8789
this._hasGuestIdentityId = false;
8890
}
8991
}

0 commit comments

Comments
 (0)