Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWA-2571: Regression fix disabled TTL support in BrowserPersistence #3729

Merged
merged 5 commits into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;

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