-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PasswordForm.js
executable file
·160 lines (140 loc) · 5.65 KB
/
PasswordForm.js
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
import React from 'react';
import {
TouchableOpacity, View,
} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import styles from '../../styles/styles';
import Button from '../../components/Button';
import Text from '../../components/Text';
import themeColors from '../../styles/themes/default';
import {signIn, resetPassword} from '../../libs/actions/Session';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import ChangeExpensifyLoginLink from './ChangeExpensifyLoginLink';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import ExpensiTextInput from '../../components/ExpensiTextInput';
const propTypes = {
/* Onyx Props */
/** The details about the account that the user is signing in with */
account: PropTypes.shape({
/** Whether or not the account already exists */
accountExists: PropTypes.bool,
/** Whether or not two factor authentication is required */
requiresTwoFactorAuth: PropTypes.bool,
/** Whether or not a sign on form is loading (being submitted) */
loading: PropTypes.bool,
}),
...withLocalizePropTypes,
};
const defaultProps = {
account: {},
};
class PasswordForm extends React.Component {
constructor(props) {
super(props);
this.validateAndSubmitForm = this.validateAndSubmitForm.bind(this);
this.state = {
formError: false,
password: '',
twoFactorAuthCode: '',
};
}
/**
* Check that all the form fields are valid, then trigger the submit callback
*/
validateAndSubmitForm() {
if (!this.state.password.trim() && this.props.account.requiresTwoFactorAuth && !this.state.twoFactorAuthCode.trim()) {
this.setState({formError: 'passwordForm.pleaseFillOutAllFields'});
return;
}
if (!this.state.password.trim()) {
this.setState({formError: 'passwordForm.pleaseFillPassword'});
return;
}
if (this.props.account.requiresTwoFactorAuth && !this.state.twoFactorAuthCode.trim()) {
this.setState({formError: 'passwordForm.pleaseFillTwoFactorAuth'});
return;
}
this.setState({
formError: null,
});
signIn(this.state.password, this.state.twoFactorAuthCode);
}
render() {
return (
<>
<View style={[styles.mv3]}>
<ExpensiTextInput
label={this.props.translate('common.password')}
secureTextEntry
autoCompleteType="password"
textContentType="password"
value={this.state.password}
onChangeText={text => this.setState({password: text})}
onSubmitEditing={this.validateAndSubmitForm}
autoFocus
translateX={-18}
blurOnSubmit={false}
/>
<View style={[styles.changeExpensifyLoginLinkContainer]}>
<TouchableOpacity
style={[styles.mt2]}
onPress={resetPassword}
underlayColor={themeColors.componentBG}
>
<Text style={[styles.link]}>
{this.props.translate('passwordForm.forgot')}
</Text>
</TouchableOpacity>
</View>
</View>
{this.props.account.requiresTwoFactorAuth && (
<View style={[styles.mv3]}>
<ExpensiTextInput
label={this.props.translate('passwordForm.twoFactorCode')}
value={this.state.twoFactorAuthCode}
placeholder={this.props.translate('passwordForm.requiredWhen2FAEnabled')}
placeholderTextColor={themeColors.placeholderText}
onChangeText={text => this.setState({twoFactorAuthCode: text})}
onSubmitEditing={this.validateAndSubmitForm}
keyboardType={CONST.KEYBOARD_TYPE.NUMERIC}
translateX={-18}
blurOnSubmit={false}
/>
</View>
)}
{!this.state.formError && this.props.account && !_.isEmpty(this.props.account.error) && (
<Text style={[styles.formError]}>
{this.props.account.error}
</Text>
)}
{this.state.formError && (
<Text style={[styles.formError]}>
{this.props.translate(this.state.formError)}
</Text>
)}
<View>
<Button
success
style={[styles.mv3]}
text={this.props.translate('common.signIn')}
isLoading={this.props.account.loading}
onPress={this.validateAndSubmitForm}
/>
<ChangeExpensifyLoginLink />
</View>
</>
);
}
}
PasswordForm.propTypes = propTypes;
PasswordForm.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
account: {key: ONYXKEYS.ACCOUNT},
}),
)(PasswordForm);