Advertisement
Guest User

Untitled

a guest
Jan 13th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2. import { StyleSheet, Text, View, TextInput, TouchableOpacity, AsyncStorage } from 'react-native';
  3. import { withNavigation } from 'react-navigation';
  4. import { NetInfo } from 'react-native'
  5.  
  6.  
  7. const serverUrl = "http://192.168.43.108:8080/users";
  8.  
  9.  
  10.  
  11. class LoginComponent extends React.Component{
  12.  
  13.  
  14.     constructor(props) {
  15.       super(props);
  16.       this.state = {username: '',
  17.                     password: '',
  18.                     isConnected: true,
  19.                     users: []
  20.                   };
  21.       AsyncStorage.setItem('users', JSON.stringify(this.state.users));
  22.     }
  23.  
  24.    
  25.    handleConnectivityChange = (isConnected) => {
  26.     this.setState({ isConnected });
  27.   }
  28.  
  29.     componentDidMount(){
  30.       NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
  31.     }
  32.  
  33.     getUser(username, password){
  34.       var handleLogin = this.props.handleLogin;
  35.       const path = `${serverUrl}?username=${username}`;
  36.       const axios = require('axios');
  37.  
  38.       if (this.state.isConnected === true){
  39.         axios.get(path)
  40.         .then((user) => {
  41.                           if (user.data.password === password){
  42.                             handleLogin({loggedIn: 'true'});
  43.                             this.setState({users: [...this.state.users, user.data]});
  44.                             AsyncStorage.setItem('users', JSON.stringify(this.state.users));
  45.                             this.props.navigation.navigate('mainscreen');
  46.                           }
  47.                           else{
  48.                              alert("Wrong username or password!");
  49.                           }
  50.                         })
  51.           .catch(function(error) {
  52.               alert("Network problem!")
  53.           });
  54.  
  55.       }
  56.       else{
  57.         AsyncStorage.getItem('users')
  58.         .then((response) => {
  59.           let responses = JSON.parse(response);
  60.            let user = responses.filter(function(response){
  61.              return response.email === username && response.password === password;
  62.            });
  63.            
  64.            if (user.length > 0){
  65.             this.props.navigation.navigate('mainscreen');
  66.            }
  67.            else {
  68.              alert("There is no such user in our local database!");
  69.            }
  70.         })
  71.         .catch(error => {
  72.           alert(error);
  73.         })
  74.  
  75.       }
  76.     }
  77.  
  78.     render(){
  79.         return (
  80.             <View style={styles.container}>
  81.  
  82.             <Text style={styles.text}>LogIn</Text>
  83.             <TextInput style={styles.input}
  84.                        value={this.state.username}
  85.                        onChangeText = {(text) => this.setState({username: text})}
  86.                        placeholder="Enter username" />
  87.            
  88.             <TextInput style={styles.input}
  89.                        secureTextEntry={true}
  90.                        value={this.state.password}
  91.                        onChangeText = {(text) => this.setState({password: text})}
  92.                        placeholder="Enter password"/>
  93.            
  94.             <TouchableOpacity style={styles.button}
  95.                    onPress={() => this.getUser(this.state.username, this.state.password)}
  96.             >
  97.               <Text style={styles.text}>Press me</Text>
  98.             </TouchableOpacity>
  99.         </View>
  100.         );
  101.     }
  102. }
  103.  
  104.  
  105. const styles = StyleSheet.create({
  106.     container: {
  107.       flex: 1,
  108.       flexDirection: 'column',
  109.       backgroundColor: '#fff',
  110.       alignItems: 'stretch',
  111.       justifyContent: 'center',
  112.     },
  113.     input: {
  114.       margin: 15,
  115.       height: 40,
  116.       borderColor: '#7a42f4',
  117.       borderWidth: 1,
  118.       paddingLeft: 20
  119.    },
  120.    button: {
  121.      padding: 10,
  122.      margin: 15,
  123.      borderRadius: 10,
  124.      backgroundColor: "#7CFC00",
  125.      alignContent: "center"
  126.    },
  127.    text: {
  128.     textAlign: "center",
  129.    
  130.    }
  131.   });
  132.  
  133.  
  134. export default  withNavigation(LoginComponent)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement