Skip to content

Commit

Permalink
PWA-2571: Regression fix disabled TTL support in BrowserPersistence (#…
Browse files Browse the repository at this point in the history
…3729)

* PWA-2571: Regression fix disabled TTL support in BrowserPersistence

- Reverted ttl support for getItem

* PWA-2571: Regression fix disabled TTL support in BrowserPersistence

- Updated user reducers to not use the ttl checking getItem to let user component validate.

Co-authored-by: Tommy Wiebell <twiebell@adobe.com>
  • Loading branch information
michaelyu0123 and tjwiebell committed Mar 23, 2022
1 parent f145f1d commit 5aae89a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/peregrine/lib/store/reducers/user.js
Expand Up @@ -7,7 +7,17 @@ import actions from '../actions/user';

export const name = 'user';

const isSignedIn = () => !!storage.getItem('signin_token');
const rawSignInToken = storage.getRawItem('signin_token');

const isSignedIn = () => !!rawSignInToken;

This comment has been minimized.

Copy link
@morenopl

morenopl Aug 31, 2023

This change causes issue: on page reload token gets additional quote marks:
image

I think that token should be returned as previously with use of storage.getItem('signin_token'):
image


const getToken = () => {
if (!rawSignInToken) {
return undefined;
}
const { value } = JSON.parse(rawSignInToken);
return value;
};

const initialState = {
currentUser: {
Expand All @@ -20,7 +30,7 @@ const initialState = {
isResettingPassword: false,
isSignedIn: isSignedIn(),
resetPasswordError: null,
token: storage.getItem('signin_token')
token: getToken()
};

const reducerMap = {
Expand Down
2 changes: 2 additions & 0 deletions packages/peregrine/lib/util/__mocks__/simplePersistence.js
@@ -1,9 +1,11 @@
export const mockGetItem = jest.fn();
export const mockGetRawItem = jest.fn();
export const mockRemoveItem = jest.fn();
export const mockSetItem = jest.fn();

export const BrowserPersistence = jest.fn().mockImplementation(() => ({
getItem: mockGetItem,
getRawItem: mockGetRawItem,
removeItem: mockRemoveItem,
setItem: mockSetItem
}));
Expand Down
18 changes: 18 additions & 0 deletions packages/peregrine/lib/util/__tests__/simplePersistence.spec.js
Expand Up @@ -59,6 +59,24 @@ describe('getItem', () => {
});
});

test('it removes the item and returns undefined if the item has expired', () => {
// Test setup: an expired item.
mockGetItem.mockImplementationOnce(() =>
shape({
value: MOCK_VALUE,
timeStored: 0,
ttl: 1
})
);

// Call the function.
const result = instance.getItem(NAME);

// Make assertions.
expect(mockRemoveItem).toHaveBeenCalledWith(KEY);
expect(result).toBeUndefined();
});

describe('setItem', () => {
test('it puts the item in storage with the correct values', () => {
// Call the function.
Expand Down
8 changes: 7 additions & 1 deletion packages/peregrine/lib/util/simplePersistence.js
Expand Up @@ -42,11 +42,17 @@ export default class BrowserPersistence {
return this.storage.getItem(name);
}
getItem(name) {
const now = Date.now();
const item = this.storage.getItem(name);
if (!item) {
return undefined;
}
const { value } = JSON.parse(item);
const { value, ttl, timeStored } = JSON.parse(item);

if (ttl && now - timeStored > ttl * 1000) {
this.storage.removeItem(name);
return undefined;
}

return JSON.parse(value);
}
Expand Down

0 comments on commit 5aae89a

Please sign in to comment.