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

Remember session checkbox state on login #227

Merged
merged 7 commits into from
Aug 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/NFT.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ describe('Mint', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/networks.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ describe('Switch networks', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/offers.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ describe('Offers', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
36 changes: 36 additions & 0 deletions packages/extension/cypress/e2e/payments.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ describe('Make payment - XRP', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
}
Expand Down Expand Up @@ -127,6 +136,15 @@ describe('Make payment - ETH', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
}
Expand Down Expand Up @@ -218,6 +236,15 @@ describe('Make payment - SOLO', () => {
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};
}
}
);
Expand Down Expand Up @@ -256,6 +283,15 @@ describe('Make payment - SOLO', () => {
(win as any).chrome.runtime = {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};
}
}
);
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/send_token.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('Send Token', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/set_account.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('Set Account', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/submit_transaction.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ describe('Submit Transaction', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
9 changes: 9 additions & 0 deletions packages/extension/cypress/e2e/trustlines.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ const navigate = (url: string, password: string) => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
45 changes: 45 additions & 0 deletions packages/extension/cypress/e2e/wallet_management.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ describe('Setup the initial wallet (no previous wallet)', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down Expand Up @@ -239,6 +248,15 @@ describe('Add an additional wallet (with previous wallet)', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down Expand Up @@ -400,6 +418,15 @@ describe('Edit wallet', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down Expand Up @@ -539,6 +566,15 @@ describe('Switch wallet', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down Expand Up @@ -595,6 +631,15 @@ describe('Reset password', () => {
sendMessage(message, cb) {}
};

(win as any).chrome.storage = {
local: {
get(key, cb) {},
set(obj, cb) {
if (cb) cb();
}
}
};

cy.stub((win as any).chrome.runtime, 'sendMessage').resolves({});
}
});
Expand Down
19 changes: 17 additions & 2 deletions packages/extension/src/components/pages/Login/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, FC, useCallback, useRef } from 'react';
import { useState, useEffect, FC, useCallback, useRef, ChangeEvent } from 'react';

import {
Button,
Expand Down Expand Up @@ -52,6 +52,7 @@ import {
import { useWallet } from '../../../contexts';
import { useKeyUp } from '../../../hooks/useKeyUp';
import { loadData } from '../../../utils';
import { loadRememberSessionState, saveRememberSessionState } from '../../../utils/login';
import { Logo } from '../../atoms/Logo';

export const Login: FC = () => {
Expand Down Expand Up @@ -108,9 +109,18 @@ export const Login: FC = () => {
}
}, [navigate, navigateToPath, search, selectedWallet, wallets]);

useEffect(() => {
const loadRememberSession = async () => {
const loadedState = await loadRememberSessionState();
setRememberSession(loadedState);
};
loadRememberSession();
}, []);

const handleUnlock = useCallback(() => {
const passwordValue = passwordRef.current?.value;
if (passwordValue && signIn(passwordValue, rememberSession)) {
saveRememberSessionState(rememberSession);
navigateToPath();

if (process.env.NODE_ENV === 'production') {
Expand Down Expand Up @@ -146,6 +156,11 @@ export const Login: FC = () => {
navigate(RESET_PASSWORD_PATH);
}, [navigate]);

const handleRememberSessionChange = (e: ChangeEvent<HTMLInputElement>) => {
const checked = e.target.checked;
setRememberSession(checked);
};

return (
<Container
component="main"
Expand Down Expand Up @@ -191,7 +206,7 @@ export const Login: FC = () => {
control={
<Checkbox
checked={rememberSession}
onChange={(e) => setRememberSession(e.target.checked)}
onChange={handleRememberSessionChange}
name="rememberSession"
color="primary"
style={{ transform: 'scale(0.9)' }}
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './apiErrors';
export * from './colors';
export * from './links';
export * from './localStorage';
export * from './storage';
export * from './navigation';
export * from './parameters';
export * from './routes';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const STORAGE_SEED = 'seed';
export const STORAGE_TRUSTED_APPS = 'trustedApps';
export const STORAGE_WALLETS = 'wallets';
export const STORAGE_SELECTED_WALLET = 'selectedWallet';
export const STORAGE_REMEMBER_SESSION = 'rememberSession';
3 changes: 2 additions & 1 deletion packages/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export * from './link';
export * from './network';
export * from './numbersToSeed';
export * from './parseFromString';
export * from './storage';
export * from './storageChrome';
export * from './storageLocal';
export * from './transaction';
export * from './truncateAddress';
export * from './truncateWalletName';
Expand Down
55 changes: 55 additions & 0 deletions packages/extension/src/utils/login.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { STORAGE_REMEMBER_SESSION } from '../constants';
import { loadRememberSessionState, saveRememberSessionState } from './login';
import { saveInChromeStorage, loadFromChromeStorage } from './storageChrome';

jest.mock('./storageChrome', () => ({
saveInChromeStorage: jest.fn(),
loadFromChromeStorage: jest.fn()
}));

describe('saveRememberSessionState', () => {
beforeEach(() => {
saveInChromeStorage.mockClear();
});

test('should save the remember session state to chrome storage', async () => {
const state = true;
saveRememberSessionState(state);
expect(saveInChromeStorage).toHaveBeenCalledWith(
STORAGE_REMEMBER_SESSION,
JSON.stringify(state)
);
});
});

describe('loadRememberSessionState', () => {
beforeEach(() => {
loadFromChromeStorage.mockClear();
});

test('should load the remember session state from chrome storage', async () => {
const state = true;
(loadFromChromeStorage as jest.Mock).mockResolvedValue({
[STORAGE_REMEMBER_SESSION]: JSON.stringify(state)
});

const loadedState = await loadRememberSessionState();
expect(loadedState).toEqual(state);
});

test('should return false if no state is found in chrome storage', async () => {
(loadFromChromeStorage as jest.Mock).mockResolvedValue({});

const loadedState = await loadRememberSessionState();
expect(loadedState).toEqual(false);
});

test('should return false if an error occurs while loading the state', async () => {
(loadFromChromeStorage as jest.Mock).mockRejectedValue(
new Error('Error loading state from chrome storage')
);

const loadedState = await loadRememberSessionState();
expect(loadedState).toEqual(false);
});
});
Loading