Commit 5b8b75b4 authored by Lihinikaduwa D.N.R.  's avatar Lihinikaduwa D.N.R.

Login changed

parent 55d2d8ec
import {useNavigation} from '@react-navigation/native'; import {useNavigation} from '@react-navigation/native';
import Orientation from 'react-native-orientation-locker'; import Orientation from 'react-native-orientation-locker';
import React, { useState } from "react"; import React, {useState} from 'react';
import { SafeAreaView, ScrollView, View, StyleSheet, ImageBackground, Text, TextInput, TouchableOpacity, Image } from "react-native"; import {
import {useSelector, useDispatch} from 'react-redux'; SafeAreaView,
import {userLogin} from '../../store/actions/login'; ScrollView,
View,
import client from "../client/Client"; StyleSheet,
ImageBackground,
const isValidObjectField = (obj) => { Text,
TextInput,
TouchableOpacity,
Image,
} from 'react-native';
import client from '../client/Client';
const isValidObjectField = obj => {
return Object.values(obj).every(value => value.trim()); return Object.values(obj).every(value => value.trim());
} };
const updateError = (error, stateUpdater) => { const updateError = (error, stateUpdater) => {
stateUpdater(error); stateUpdater(error);
...@@ -24,16 +32,10 @@ const isValidEmail = value => { ...@@ -24,16 +32,10 @@ const isValidEmail = value => {
}; };
const Login = () => { const Login = () => {
const navigation = useNavigation();
const dispatch = useDispatch();
const users = useSelector(state=>state.users.users);
const navigation = useNavigation(); const navigation = useNavigation();
React.useEffect(() => { React.useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => { const unsubscribe = navigation.addListener('focus', () => {
Orientation.unlockAllOrientations(); Orientation.unlockAllOrientations();
Orientation.lockToPortrait(); Orientation.lockToPortrait();
}); });
...@@ -44,13 +46,6 @@ const Login = () => { ...@@ -44,13 +46,6 @@ const Login = () => {
email: '', email: '',
password: '', password: '',
}); });
return unsubscribe;
}, [navigation]);
const [userInfo, setUserInfo] = useState({
email: '',
password: '',
});
const {email, password} = userInfo; const {email, password} = userInfo;
...@@ -67,63 +62,103 @@ const Login = () => { ...@@ -67,63 +62,103 @@ const Login = () => {
return updateError('Required all fields!', setError); return updateError('Required all fields!', setError);
} }
const loginHandler = () => { if (!isValidEmail(email)) {
const loggedInUser = users.find(user=>user.email === email && user.password === password) return updateError('Invalid email !', setError);
if(loggedInUser){
console.log(loggedInUser);
dispatch(userLogin({
name: loggedInUser.name,
id: loggedInUser.uid
}));
navigation.navigate('Home');
}else{
console.log('Invalid user credentials...');
} }
if (!password.trim() || password.length < 8) {
return updateError('Password is less than 8 characters !', setError);
} }
return true;
};
const submitForm = () => { const submitForm = () => {
if (isValidForm()){ if (isValidForm()) {
loginFun(userInfo);
}
};
loginFun(userInfo) const loginFun = () => {
var formData = JSON.stringify(userInfo);
} client
} .post('login', formData, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
.then(response => {
console.log(response.data);
if (!password.trim() || password.length < 8) { if (response.data.status == 404) {
return updateError('Password is less than 8 characters !', setError); return updateError('Please register our system', setError);
} }
if (response.data.status == 200) {
return navigation.navigate('Start');
}
})
.catch(err => {
console.log(err);
});
};
return( return (
<SafeAreaView> <SafeAreaView>
<ScrollView> <ScrollView>
<View style={styles.continer}> <View style={styles.continer}>
<ImageBackground style={styles.backgroundImage} source={require('../../assets/login/login_background.png')} resizeMode="cover"> <ImageBackground
style={styles.backgroundImage}
source={require('../../assets/login/login_background.png')}
resizeMode="cover">
<View> <View>
<Image style={styles.logo} source={require('../../assets/login/logo1.png')} resizeMode="contain"></Image> <Image
style={styles.logo}
source={require('../../assets/login/logo1.png')}
resizeMode="contain"></Image>
</View> </View>
<View elevation={5} style={styles.main_container}> <View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text> <Text style={styles.main_title}>Sign In</Text>
{error ? (<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>{error}</Text>) : null} {error ? (
<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>
{error}
</Text>
) : null}
<View style={styles.form_input}> <View style={styles.form_input}>
<TextInput id="email" value={email} autoCapitalize="none" onChangeText={value => handleOnChangeText(value, 'email')} style={styles.text_input} placeholder="Enter Email"></TextInput> <TextInput
id="email"
value={email}
autoCapitalize="none"
onChangeText={value => handleOnChangeText(value, 'email')}
style={styles.text_input}
placeholder="Enter Email"></TextInput>
</View> </View>
<View style={styles.form_input}> <View style={styles.form_input}>
<TextInput id="password" value={password} autoCapitalize="none" secureTextEntry onChangeText={value => handleOnChangeText(value, 'password')} style={styles.text_input} placeholder="Enter Password"></TextInput> <TextInput
id="password"
value={password}
autoCapitalize="none"
secureTextEntry
onChangeText={value => handleOnChangeText(value, 'password')}
style={styles.text_input}
placeholder="Enter Password"></TextInput>
</View> </View>
<View style={styles.form_input}> <View style={styles.form_input}>
<TouchableOpacity onPress={ ()=>{navigation.navigate("Start");}} style={styles.btn}> <TouchableOpacity onPress={submitForm} style={styles.btn}>
<Text style={styles.btn_text}> <Text style={styles.btn_text}>Sign In</Text>
Sign In
</Text>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
{/* onPress={submitForm} */} {/* onPress={submitForm} */}
<View style={styles.text_if}> <View style={styles.text_if}>
<TouchableOpacity onPress={()=> { navigation.navigate("Register")}}> <TouchableOpacity
onPress={() => {
navigation.navigate('Register');
}}>
<Text style={styles.btn_text2}> <Text style={styles.btn_text2}>
If you don't have account? Sign Up If you don't have account? Sign Up
</Text> </Text>
...@@ -134,8 +169,8 @@ const Login = () => { ...@@ -134,8 +169,8 @@ const Login = () => {
</View> </View>
</ScrollView> </ScrollView>
</SafeAreaView> </SafeAreaView>
) );
} };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
continer: { continer: {
......
...@@ -2,6 +2,6 @@ import axios from 'axios'; ...@@ -2,6 +2,6 @@ import axios from 'axios';
// export default axios.create({ baseURL: 'http://192.168.8.102:5000/', timeout: 15000, }); // export default axios.create({ baseURL: 'http://192.168.8.102:5000/', timeout: 15000, });
export default axios.create({ export default axios.create({
baseURL: 'http://192.168.1.101:5000/', baseURL: 'http://192.168.1.100:5000/',
timeout: 15000, timeout: 15000,
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment