-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathCodesStep.tsx
193 lines (183 loc) · 10.5 KB
/
CodesStep.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type {RouteProp} from '@react-navigation/native';
import {useRoute} from '@react-navigation/native';
import React, {useEffect, useMemo, useState} from 'react';
import {ActivityIndicator, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import FixedFooter from '@components/FixedFooter';
import FormHelpMessage from '@components/FormHelpMessage';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
import PressableWithDelayToggle from '@components/Pressable/PressableWithDelayToggle';
import ScrollView from '@components/ScrollView';
import Section from '@components/Section';
import Text from '@components/Text';
import ValidateCodeActionModal from '@components/ValidateCodeActionModal';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {READ_COMMANDS} from '@libs/API/types';
import Clipboard from '@libs/Clipboard';
import * as ErrorUtils from '@libs/ErrorUtils';
import localFileDownload from '@libs/localFileDownload';
import type {BackToParams, SettingsNavigatorParamList} from '@libs/Navigation/types';
import StepWrapper from '@pages/settings/Security/TwoFactorAuth/StepWrapper/StepWrapper';
import useTwoFactorAuthContext from '@pages/settings/Security/TwoFactorAuth/TwoFactorAuthContext/useTwoFactorAuth';
import * as Session from '@userActions/Session';
import * as TwoFactorAuthActions from '@userActions/TwoFactorAuthActions';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type CodesStepProps = BackToParams;
function CodesStep({backTo}: CodesStepProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to use correct style
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isExtraSmallScreenWidth, isSmallScreenWidth} = useResponsiveLayout();
const [error, setError] = useState('');
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [user] = useOnyx(ONYXKEYS.USER);
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const [validateCodeAction] = useOnyx(ONYXKEYS.VALIDATE_ACTION_CODE);
const isUserValidated = user?.validated;
const contactMethod = account?.primaryLogin ?? '';
const route = useRoute<RouteProp<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.TWO_FACTOR_AUTH>>();
const loginData = useMemo(() => loginList?.[contactMethod], [loginList, contactMethod]);
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
const hasMagicCodeBeenSent = !!validateCodeAction?.validateCodeSent;
const {setStep} = useTwoFactorAuthContext();
useEffect(() => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (account?.requiresTwoFactorAuth || account?.recoveryCodes || !isUserValidated) {
return;
}
Session.toggleTwoFactorAuth(true);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- We want to run this when component mounts
}, [isUserValidated]);
return (
<StepWrapper
title={translate('twoFactorAuth.headerTitle')}
shouldEnableKeyboardAvoidingView={false}
stepCounter={{
step: 1,
text: translate('twoFactorAuth.stepCodes'),
total: 3,
}}
// When the 2FA code step is open from Xero flow, we don't need to pass backTo because we build the necessary root route
// from the backTo param in the route (in getMatchingRootRouteForRHPRoute) and goBack will not need a fallbackRoute.
onBackButtonPress={() => TwoFactorAuthActions.quitAndNavigateBack(route?.params?.forwardTo?.includes(READ_COMMANDS.CONNECT_POLICY_TO_XERO) ? '' : backTo)}
>
<ScrollView contentContainerStyle={styles.flexGrow1}>
{!!isUserValidated && (
<Section
title={translate('twoFactorAuth.keepCodesSafe')}
icon={Illustrations.ShieldYellow}
containerStyles={[styles.twoFactorAuthSection]}
iconContainerStyles={[styles.ml6]}
>
<View style={styles.mv3}>
<Text>{translate('twoFactorAuth.codesLoseAccess')}</Text>
</View>
<View style={styles.twoFactorAuthCodesBox({isExtraSmallScreenWidth, isSmallScreenWidth})}>
{account?.isLoading ? (
<View style={styles.twoFactorLoadingContainer}>
<ActivityIndicator color={theme.spinner} />
</View>
) : (
<>
<View style={styles.twoFactorAuthCodesContainer}>
{!!account?.recoveryCodes &&
account?.recoveryCodes?.split(', ').map((code) => (
<Text
style={styles.twoFactorAuthCode}
key={code}
>
{code}
</Text>
))}
</View>
<View style={styles.twoFactorAuthCodesButtonsContainer}>
<PressableWithDelayToggle
text={translate('twoFactorAuth.copy')}
textChecked={translate('common.copied')}
icon={Expensicons.Copy}
inline={false}
onPress={() => {
Clipboard.setString(account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=""
tooltipTextChecked=""
/>
<PressableWithDelayToggle
text={translate('common.download')}
icon={Expensicons.Download}
onPress={() => {
localFileDownload('two-factor-auth-codes', account?.recoveryCodes ?? '');
setError('');
TwoFactorAuthActions.setCodesAreCopied();
}}
inline={false}
styles={[styles.button, styles.buttonMedium, styles.twoFactorAuthCodesButton]}
textStyles={[styles.buttonMediumText]}
accessible={false}
tooltipText=""
tooltipTextChecked=""
/>
</View>
</>
)}
</View>
</Section>
)}
<FixedFooter style={[styles.mtAuto, styles.pt5]}>
{!!error && (
<FormHelpMessage
isError
message={error}
style={[styles.mb3]}
/>
)}
<Button
success
large
isDisabled={!isUserValidated}
text={translate('common.next')}
onPress={() => {
if (!account?.codesAreCopied) {
setError(translate('twoFactorAuth.errorStepCodes'));
return;
}
setStep(CONST.TWO_FACTOR_AUTH_STEPS.VERIFY);
}}
/>
</FixedFooter>
<ValidateCodeActionModal
title={translate('contacts.validateAccount')}
descriptionPrimary={translate('contacts.featureRequiresValidate')}
descriptionSecondary={translate('contacts.enterMagicCode', {contactMethod})}
isVisible={!isUserValidated}
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
validatePendingAction={loginData?.pendingFields?.validateCodeSent}
sendValidateCode={() => User.requestValidateCodeAction()}
handleSubmitForm={(validateCode) => User.validateSecondaryLogin(loginList, contactMethod, validateCode, true)}
validateError={!isEmptyObject(validateLoginError) ? validateLoginError : ErrorUtils.getLatestErrorField(loginData, 'validateCodeSent')}
clearError={() => User.clearContactMethodErrors(contactMethod, !isEmptyObject(validateLoginError) ? 'validateLogin' : 'validateCodeSent')}
onModalHide={() => {}}
onClose={() => TwoFactorAuthActions.quitAndNavigateBack(backTo)}
/>
</ScrollView>
</StepWrapper>
);
}
CodesStep.displayName = 'CodesStep';
export default CodesStep;