Commit 202280be authored by Lihinikaduwa D.N.R.  's avatar Lihinikaduwa D.N.R.

Merge branch 'it18257632' into 'master'

Login changed

See merge request !145
parents 55d2d8ec 5b8b75b4
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,
return Object.values(obj).every(value => value.trim()); TextInput,
} TouchableOpacity,
Image,
} from 'react-native';
import client from '../client/Client';
const isValidObjectField = obj => {
return Object.values(obj).every(value => value.trim());
};
const updateError = (error, stateUpdater) => { const updateError = (error, stateUpdater) => {
stateUpdater(error); stateUpdater(error);
...@@ -26,23 +34,10 @@ const isValidEmail = value => { ...@@ -26,23 +34,10 @@ const isValidEmail = value => {
const Login = () => { const Login = () => {
const navigation = useNavigation(); const navigation = useNavigation();
const dispatch = useDispatch(); React.useEffect(() => {
const unsubscribe = navigation.addListener('focus', () => {
const users = useSelector(state=>state.users.users); Orientation.unlockAllOrientations();
Orientation.lockToPortrait();
const navigation = useNavigation();
React.useEffect(() => {
const unsubscribe = navigation.addListener("focus", () => {
Orientation.unlockAllOrientations();
Orientation.lockToPortrait();
});
return unsubscribe;
}, [navigation]);
const [userInfo, setUserInfo] = useState({
email: '',
password: '',
}); });
return unsubscribe; return unsubscribe;
}, [navigation]); }, [navigation]);
...@@ -67,75 +62,115 @@ const Login = () => { ...@@ -67,75 +62,115 @@ 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...');
}
}
const submitForm = () => {
if (isValidForm()){
loginFun(userInfo)
}
} }
if (!password.trim() || password.length < 8) { if (!password.trim() || password.length < 8) {
return updateError('Password is less than 8 characters !', setError); return updateError('Password is less than 8 characters !', setError);
} }
return( return true;
<SafeAreaView> };
<ScrollView>
<View style={styles.continer}> const submitForm = () => {
<ImageBackground style={styles.backgroundImage} source={require('../../assets/login/login_background.png')} resizeMode="cover"> if (isValidForm()) {
<View> loginFun(userInfo);
<Image style={styles.logo} source={require('../../assets/login/logo1.png')} resizeMode="contain"></Image> }
</View> };
<View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text> const loginFun = () => {
var formData = JSON.stringify(userInfo);
{error ? (<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>{error}</Text>) : null}
client
<View style={styles.form_input}> .post('login', formData, {
<TextInput id="email" value={email} autoCapitalize="none" onChangeText={value => handleOnChangeText(value, 'email')} style={styles.text_input} placeholder="Enter Email"></TextInput> headers: {
</View> Accept: 'application/json',
'Content-Type': 'application/json',
<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> })
</View> .then(response => {
console.log(response.data);
<View style={styles.form_input}>
<TouchableOpacity onPress={ ()=>{navigation.navigate("Start");}} style={styles.btn}> if (response.data.status == 404) {
<Text style={styles.btn_text}> return updateError('Please register our system', setError);
Sign In }
</Text> if (response.data.status == 200) {
</TouchableOpacity> return navigation.navigate('Start');
</View> }
{/* onPress={submitForm} */} })
<View style={styles.text_if}> .catch(err => {
<TouchableOpacity onPress={()=> { navigation.navigate("Register")}}> console.log(err);
<Text style={styles.btn_text2}> });
If you don't have account? Sign Up };
</Text>
</TouchableOpacity> return (
</View> <SafeAreaView>
</View> <ScrollView>
</ImageBackground> <View style={styles.continer}>
</View> <ImageBackground
</ScrollView> style={styles.backgroundImage}
</SafeAreaView> source={require('../../assets/login/login_background.png')}
) resizeMode="cover">
} <View>
<Image
style={styles.logo}
source={require('../../assets/login/logo1.png')}
resizeMode="contain"></Image>
</View>
<View elevation={5} style={styles.main_container}>
<Text style={styles.main_title}>Sign In</Text>
{error ? (
<Text style={{color: 'red', fontSize: 18, textAlign: 'center'}}>
{error}
</Text>
) : null}
<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>
</View>
<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>
</View>
<View style={styles.form_input}>
<TouchableOpacity onPress={submitForm} style={styles.btn}>
<Text style={styles.btn_text}>Sign In</Text>
</TouchableOpacity>
</View>
{/* onPress={submitForm} */}
<View style={styles.text_if}>
<TouchableOpacity
onPress={() => {
navigation.navigate('Register');
}}>
<Text style={styles.btn_text2}>
If you don't have account? Sign Up
</Text>
</TouchableOpacity>
</View>
</View>
</ImageBackground>
</View>
</ScrollView>
</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