Refactor
This commit is contained in:
parent
8d5efcd3f8
commit
788b0ddf9a
|
@ -0,0 +1,7 @@
|
|||
export const PASSCODE_KEY = 'kPasscode';
|
||||
export const LOCKED_OUT_TIMER_KEY = 'kLockedOutTimer';
|
||||
export const ATTEMPTS_KEY = 'kAttempts';
|
||||
|
||||
export const LOCAL_AUTHENTICATE_EMITTER = 'LOCAL_AUTHENTICATE';
|
||||
|
||||
export const PASSCODE_LENGTH = 6;
|
|
@ -1,3 +0,0 @@
|
|||
export const PASSCODE_KEY = 'kPasscode';
|
||||
|
||||
export const PASSCODE_LENGTH = 6;
|
|
@ -1,11 +1,12 @@
|
|||
import * as LocalAuthentication from 'expo-local-authentication';
|
||||
import moment from 'moment';
|
||||
import RNBootSplash from 'react-native-bootsplash';
|
||||
import AsyncStorage from '@react-native-community/async-storage';
|
||||
|
||||
import database from '../lib/database';
|
||||
import { isIOS } from './deviceInfo';
|
||||
import EventEmitter from './events';
|
||||
import { LOCAL_AUTHENTICATE } from '../views/ScreenLockedView';
|
||||
import RNBootSplash from 'react-native-bootsplash';
|
||||
import { LOCAL_AUTHENTICATE_EMITTER, LOCKED_OUT_TIMER_KEY, ATTEMPTS_KEY } from '../constants/localAuthentication';
|
||||
|
||||
export const saveLastLocalAuthenticationSession = async(server, serverRecord) => {
|
||||
const serversDB = database.servers;
|
||||
|
@ -25,8 +26,10 @@ export const saveLastLocalAuthenticationSession = async(server, serverRecord) =>
|
|||
});
|
||||
};
|
||||
|
||||
export const resetAttempts = () => AsyncStorage.multiRemove([LOCKED_OUT_TIMER_KEY, ATTEMPTS_KEY]);
|
||||
|
||||
export const localPasscode = () => new Promise((resolve, reject) => {
|
||||
EventEmitter.emit(LOCAL_AUTHENTICATE, {
|
||||
EventEmitter.emit(LOCAL_AUTHENTICATE_EMITTER, {
|
||||
cancel: () => reject(),
|
||||
submit: () => resolve()
|
||||
});
|
||||
|
@ -65,6 +68,7 @@ export const localAuthenticate = async(server) => {
|
|||
// opens biometry prompt
|
||||
const authResult = await LocalAuthentication.authenticateAsync({ disableDeviceFallback: true });
|
||||
if (authResult?.success) {
|
||||
await resetAttempts();
|
||||
await saveLastLocalAuthenticationSession(server, serverRecord);
|
||||
} else {
|
||||
await localPasscode();
|
||||
|
|
|
@ -9,7 +9,7 @@ import { themedHeader } from '../utils/navigation';
|
|||
import { withTheme } from '../theme';
|
||||
import { themes } from '../constants/colors';
|
||||
import sharedStyles from './Styles';
|
||||
import { PASSCODE_KEY, PASSCODE_LENGTH } from '../constants/passcode';
|
||||
import { PASSCODE_KEY, PASSCODE_LENGTH } from '../constants/localAuthentication';
|
||||
|
||||
const ScreenLockConfigView = React.memo(({ navigation, theme }) => {
|
||||
const savePasscode = async(passcode) => {
|
||||
|
|
|
@ -19,7 +19,7 @@ import { CustomIcon } from '../lib/Icons';
|
|||
import database from '../lib/database';
|
||||
import { supportedBiometryLabel } from '../utils/localAuthentication';
|
||||
import { DisclosureImage } from '../containers/DisclosureIndicator';
|
||||
import { PASSCODE_KEY } from '../constants/passcode';
|
||||
import { PASSCODE_KEY } from '../constants/localAuthentication';
|
||||
|
||||
// RNUserDefaults.set(PASSCODE_KEY, '')
|
||||
|
||||
|
|
|
@ -15,15 +15,13 @@ import I18n from '../i18n';
|
|||
import { withTheme } from '../theme';
|
||||
import { themes } from '../constants/colors';
|
||||
import EventEmitter from '../utils/events';
|
||||
|
||||
import sharedStyles from './Styles';
|
||||
import { withSplit } from '../split';
|
||||
import { PASSCODE_KEY, PASSCODE_LENGTH } from '../constants/passcode';
|
||||
import {
|
||||
PASSCODE_KEY, PASSCODE_LENGTH, LOCAL_AUTHENTICATE_EMITTER, LOCKED_OUT_TIMER_KEY, ATTEMPTS_KEY
|
||||
} from '../constants/localAuthentication';
|
||||
import { resetAttempts } from '../utils/localAuthentication';
|
||||
|
||||
export const LOCAL_AUTHENTICATE = 'LOCAL_AUTHENTICATE';
|
||||
|
||||
const LOCKED_OUT_TIMER_KEY = 'kLockedOutTimer';
|
||||
const ATTEMPTS_KEY = 'kAttempts';
|
||||
const MAX_ATTEMPTS = 6;
|
||||
const TIME_TO_LOCK = 30000;
|
||||
|
||||
|
@ -68,14 +66,12 @@ const Timer = ({ time, theme, changeStatus }) => {
|
|||
};
|
||||
|
||||
const [timeLeft, setTimeLeft] = useState(calcTimeLeft());
|
||||
const { removeItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
setTimeLeft(calcTimeLeft());
|
||||
if (timeLeft <= 1) {
|
||||
removeItem(LOCKED_OUT_TIMER_KEY);
|
||||
removeItem(ATTEMPTS_KEY);
|
||||
resetAttempts();
|
||||
changeStatus(PinStatus.initial);
|
||||
}
|
||||
}, 1000);
|
||||
|
@ -116,7 +112,7 @@ const ScreenLockedView = ({ theme }) => {
|
|||
const [passcode, setPasscode] = useState('');
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [data, setData] = useState({});
|
||||
const { getItem, removeItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
||||
const { getItem } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);
|
||||
|
||||
useDeepCompareEffect(() => {
|
||||
if (!_.isEmpty(data)) {
|
||||
|
@ -141,16 +137,15 @@ const ScreenLockedView = ({ theme }) => {
|
|||
const lockedUntil = getLockedUntil(time);
|
||||
const diff = getDiff(lockedUntil);
|
||||
if (diff <= 1) {
|
||||
removeItem(LOCKED_OUT_TIMER_KEY);
|
||||
removeItem(ATTEMPTS_KEY);
|
||||
resetAttempts();
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
EventEmitter.addEventListener(LOCAL_AUTHENTICATE, showScreenLock);
|
||||
EventEmitter.addEventListener(LOCAL_AUTHENTICATE_EMITTER, showScreenLock);
|
||||
fetchPasscode();
|
||||
checkOldSession();
|
||||
return () => EventEmitter.removeListener(LOCAL_AUTHENTICATE);
|
||||
return () => EventEmitter.removeListener(LOCAL_AUTHENTICATE_EMITTER);
|
||||
}, []);
|
||||
|
||||
const onSubmit = () => {
|
||||
|
|
Loading…
Reference in New Issue