Skip to content

Instantly share code, notes, and snippets.

@ICeZer0
Created April 11, 2020 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ICeZer0/4b6d53b42eead48b062fc39f54d4dd54 to your computer and use it in GitHub Desktop.
Save ICeZer0/4b6d53b42eead48b062fc39f54d4dd54 to your computer and use it in GitHub Desktop.
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