Created
April 11, 2020 22:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState, useContext } from 'react'; | |
import { validateAll } from 'indicative/validator'; | |
import { View, Text } from 'react-native'; | |
import { | |
Input, | |
Card, | |
FormValidationMessage, | |
Button | |
} from 'react-native-elements'; | |
import { AuthContext } from '../../../utils/authContext'; | |
const SignInScreen = ({ navigation }) => { | |
const [emailAddress, setemailAddress] = useState(''); | |
const [password, setPassword] = useState(''); | |
const [SignUpErrors, setSignUpErrors] = useState({}); | |
const { signIn, signUp } = useContext(AuthContext); | |
const handleSignIn = () => { | |
// https://indicative.adonisjs.com | |
const rules = { | |
email: 'required|email', | |
password: 'required|string|min:6|max:40' | |
}; | |
const data = { | |
email: emailAddress, | |
password: password | |
}; | |
const messages = { | |
required: field => `${field} is required`, | |
'username.alpha': 'Username contains unallowed characters', | |
'email.email': 'Please enter a valid email address', | |
'password.min': 'Wrong Password?' | |
}; | |
validateAll(data, rules, messages) | |
.then(() => { | |
console.log('success sign in'); | |
signIn({ emailAddress, password }); | |
}) | |
.catch(err => { | |
const formatError = {}; | |
err.forEach(err => { | |
formatError[err.field] = err.message; | |
}); | |
setSignUpErrors(formatError); | |
}); | |
}; | |
return ( | |
<View> | |
<Card> | |
<Input | |
label={'Email'} | |
placeholder="Email" | |
value={emailAddress} | |
onChangeText={setemailAddress} | |
errorStyle={{ color: 'red' }} | |
errorMessage={SignUpErrors ? SignUpErrors.email : null} | |
/> | |
<Input | |
placeholder="Password" | |
value={password} | |
onChangeText={setPassword} | |
secureTextEntry | |
errorStyle={{ color: 'red' }} | |
errorMessage={SignUpErrors ? SignUpErrors.password : null} | |
/> | |
<Button | |
buttonStyle={{ margin: 10, marginTop: 50 }} | |
title="Sign in" | |
onPress={() => handleSignIn()} | |
/> | |
<Text style={{ marginLeft: 100 }} onPress={() => signUp()}> | |
No Acount? Sign Up | |
</Text> | |
</Card> | |
</View> | |
); | |
}; | |
export default SignInScreen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment