-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAuthContent.js
More file actions
83 lines (72 loc) · 2.09 KB
/
AuthContent.js
File metadata and controls
83 lines (72 loc) · 2.09 KB
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
import { useState } from 'react';
import { Alert, StyleSheet, View } from 'react-native';
import FlatButton from '../ui/FlatButton';
import AuthForm from './AuthForm';
import { Colors } from '../../constants/styles';
function AuthContent({ isLogin, onAuthenticate }) {
const [credentialsInvalid, setCredentialsInvalid] = useState({
email: false,
password: false,
confirmEmail: false,
confirmPassword: false,
});
function switchAuthModeHandler() {
// Todo
}
function submitHandler(credentials) {
let { email, confirmEmail, password, confirmPassword } = credentials;
email = email.trim();
password = password.trim();
const emailIsValid = email.includes('@');
const passwordIsValid = password.length > 6;
const emailsAreEqual = email === confirmEmail;
const passwordsAreEqual = password === confirmPassword;
if (
!emailIsValid ||
!passwordIsValid ||
(!isLogin && (!emailsAreEqual || !passwordsAreEqual))
) {
Alert.alert('Invalid input', 'Please check your entered credentials.');
setCredentialsInvalid({
email: !emailIsValid,
confirmEmail: !emailIsValid || !emailsAreEqual,
password: !passwordIsValid,
confirmPassword: !passwordIsValid || !passwordsAreEqual,
});
return;
}
onAuthenticate({ email, password });
}
return (
<View style={styles.authContent}>
<AuthForm
isLogin={isLogin}
onSubmit={submitHandler}
credentialsInvalid={credentialsInvalid}
/>
<View style={styles.buttons}>
<FlatButton onPress={switchAuthModeHandler}>
{isLogin ? 'Create a new user' : 'Log in instead'}
</FlatButton>
</View>
</View>
);
}
export default AuthContent;
const styles = StyleSheet.create({
authContent: {
marginTop: 64,
marginHorizontal: 32,
padding: 16,
borderRadius: 8,
backgroundColor: Colors.primary800,
elevation: 2,
shadowColor: 'black',
shadowOffset: { width: 1, height: 1 },
shadowOpacity: 0.35,
shadowRadius: 4,
},
buttons: {
marginTop: 8,
},
});