2020-04-01 20:32:24 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { InteractionManager, Text, View } from 'react-native';
|
2021-02-01 17:18:55 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2020-04-01 20:32:24 +00:00
|
|
|
import { sha256 } from 'js-sha256';
|
|
|
|
import Modal from 'react-native-modal';
|
|
|
|
import useDeepCompareEffect from 'use-deep-compare-effect';
|
2020-06-15 14:00:46 +00:00
|
|
|
import { connect } from 'react-redux';
|
2020-04-01 20:32:24 +00:00
|
|
|
|
2022-05-18 19:17:42 +00:00
|
|
|
import FormTextInput from '../TextInput/FormTextInput';
|
2020-04-01 20:32:24 +00:00
|
|
|
import I18n from '../../i18n';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../../lib/methods/helpers/events';
|
2022-03-22 13:44:07 +00:00
|
|
|
import { useTheme } from '../../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-04-01 20:32:24 +00:00
|
|
|
import Button from '../Button';
|
|
|
|
import sharedStyles from '../../views/Styles';
|
|
|
|
import styles from './styles';
|
2022-03-22 13:44:07 +00:00
|
|
|
import { IApplicationState } from '../../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2020-04-01 20:32:24 +00:00
|
|
|
|
|
|
|
export const TWO_FACTOR = 'TWO_FACTOR';
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
interface IMethodsProp {
|
|
|
|
text: string;
|
|
|
|
keyboardType: 'numeric' | 'default';
|
|
|
|
title?: string;
|
|
|
|
secureTextEntry?: boolean;
|
|
|
|
}
|
|
|
|
interface IMethods {
|
|
|
|
totp: IMethodsProp;
|
|
|
|
email: IMethodsProp;
|
|
|
|
password: IMethodsProp;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface EventListenerMethod {
|
|
|
|
method?: keyof IMethods;
|
|
|
|
submit?: (param: string) => void;
|
|
|
|
cancel?: () => void;
|
|
|
|
invalid?: boolean;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const methods: IMethods = {
|
2020-04-01 20:32:24 +00:00
|
|
|
totp: {
|
|
|
|
text: 'Open_your_authentication_app_and_enter_the_code',
|
|
|
|
keyboardType: 'numeric'
|
|
|
|
},
|
|
|
|
email: {
|
|
|
|
text: 'Verify_your_email_for_the_code_we_sent',
|
|
|
|
keyboardType: 'numeric'
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
title: 'Please_enter_your_password',
|
|
|
|
text: 'For_your_security_you_must_enter_your_current_password_to_continue',
|
|
|
|
secureTextEntry: true,
|
|
|
|
keyboardType: 'default'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const TwoFactor = React.memo(({ isMasterDetail }: { isMasterDetail: boolean }) => {
|
|
|
|
const { theme } = useTheme();
|
2020-04-01 20:32:24 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2022-03-22 13:44:07 +00:00
|
|
|
const [data, setData] = useState<EventListenerMethod>({});
|
|
|
|
const [code, setCode] = useState<string>('');
|
2020-04-01 20:32:24 +00:00
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const method = data.method ? methods[data.method] : null;
|
2020-04-01 20:32:24 +00:00
|
|
|
const isEmail = data.method === 'email';
|
2022-04-28 20:37:25 +00:00
|
|
|
const sendEmail = () => Services.sendEmailCode();
|
2020-04-01 20:32:24 +00:00
|
|
|
|
|
|
|
useDeepCompareEffect(() => {
|
2021-02-01 17:18:55 +00:00
|
|
|
if (!isEmpty(data)) {
|
2020-04-13 13:25:31 +00:00
|
|
|
setCode('');
|
2020-04-01 20:32:24 +00:00
|
|
|
setVisible(true);
|
|
|
|
} else {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
}, [data]);
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const showTwoFactor = (args: EventListenerMethod) => setData(args);
|
2020-04-01 20:32:24 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-23 19:00:39 +00:00
|
|
|
const listener = EventEmitter.addEventListener(TWO_FACTOR, showTwoFactor);
|
2020-04-01 20:32:24 +00:00
|
|
|
|
2021-02-23 19:00:39 +00:00
|
|
|
return () => EventEmitter.removeListener(TWO_FACTOR, listener);
|
2020-04-01 20:32:24 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const onCancel = () => {
|
|
|
|
const { cancel } = data;
|
|
|
|
if (cancel) {
|
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
setData({});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSubmit = () => {
|
|
|
|
const { submit } = data;
|
|
|
|
if (submit) {
|
|
|
|
if (data.method === 'password') {
|
|
|
|
submit(sha256(code));
|
|
|
|
} else {
|
|
|
|
submit(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setData({});
|
|
|
|
};
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const color = themes[theme].titleText;
|
2020-04-01 20:32:24 +00:00
|
|
|
return (
|
2022-03-22 13:44:07 +00:00
|
|
|
<Modal avoidKeyboard useNativeDriver isVisible={visible} hideModalContentWhileAnimating>
|
2020-05-20 16:33:40 +00:00
|
|
|
<View style={styles.container} testID='two-factor'>
|
2021-09-13 20:41:05 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.content,
|
|
|
|
isMasterDetail && [sharedStyles.modalFormSheet, styles.tablet],
|
2022-03-22 13:44:07 +00:00
|
|
|
{ backgroundColor: themes[theme].backgroundColor }
|
2021-09-13 20:41:05 +00:00
|
|
|
]}>
|
2020-04-01 20:32:24 +00:00
|
|
|
<Text style={[styles.title, { color }]}>{I18n.t(method?.title || 'Two_Factor_Authentication')}</Text>
|
2020-04-13 13:25:31 +00:00
|
|
|
{method?.text ? <Text style={[styles.subtitle, { color }]}>{I18n.t(method.text)}</Text> : null}
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-04-01 20:32:24 +00:00
|
|
|
value={code}
|
|
|
|
theme={theme}
|
2021-09-13 20:41:05 +00:00
|
|
|
inputRef={(e: any) => InteractionManager.runAfterInteractions(() => e?.getNativeRef()?.focus())}
|
2020-04-01 20:32:24 +00:00
|
|
|
returnKeyType='send'
|
|
|
|
autoCapitalize='none'
|
|
|
|
onChangeText={setCode}
|
|
|
|
onSubmitEditing={onSubmit}
|
|
|
|
keyboardType={method?.keyboardType}
|
|
|
|
secureTextEntry={method?.secureTextEntry}
|
2022-03-22 13:44:07 +00:00
|
|
|
error={data.invalid ? { error: 'totp-invalid', reason: I18n.t('Code_or_password_invalid') } : undefined}
|
2020-05-20 16:33:40 +00:00
|
|
|
testID='two-factor-input'
|
2020-04-01 20:32:24 +00:00
|
|
|
/>
|
2022-03-22 13:44:07 +00:00
|
|
|
{isEmail ? (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.sendEmail, { color }]} onPress={sendEmail}>
|
|
|
|
{I18n.t('Send_me_the_code_again')}
|
|
|
|
</Text>
|
2022-03-22 13:44:07 +00:00
|
|
|
) : null}
|
2020-04-01 20:32:24 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
|
|
|
<Button
|
|
|
|
title={I18n.t('Cancel')}
|
|
|
|
type='secondary'
|
2022-03-22 13:44:07 +00:00
|
|
|
backgroundColor={themes[theme].chatComponentBackground}
|
2020-04-01 20:32:24 +00:00
|
|
|
style={styles.button}
|
|
|
|
onPress={onCancel}
|
|
|
|
/>
|
2022-05-20 16:37:57 +00:00
|
|
|
<Button title={I18n.t('Send')} type='primary' style={styles.button} onPress={onSubmit} testID='two-factor-send' />
|
2020-04-01 20:32:24 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail
|
|
|
|
});
|
|
|
|
|
2022-03-22 13:44:07 +00:00
|
|
|
export default connect(mapStateToProps)(TwoFactor);
|