0

So I'm working my react native login in screen. I connect to the database but even putting in the right credentials I get the invalid email response.

Here's my login.php code

<?php

 // Importing DBConfig.php file.
 include 'DBConfig.php';

 // Creating connection.
 $conn = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
// Check connection
//if ($conn->connect_error) {
  //  die("Connection failed: " . $conn->connect_error);
//} else {
  //  echo ("Connected");
//}
  //$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

  // Getting the received JSON into $json variable.
  $json = file_get_contents('php://input');

  // decoding the received JSON and store into $obj variable.
  $obj = json_decode($json,true);

 // Populate User email from JSON $obj array and store into $email.
 $user_email = $obj['user_email'];

 // Populate Password from JSON $obj array and store into $password.
 $user_pass = $obj['user_pass'];

 //Applying User Login query with email and password match.
 $Sql_Query = "SELECT * FROM wp_users WHERE 'user_email' = '$user_email' AND 'user_pass' = '$user_pass' ";

 // Executing SQL Query.
 $check = mysqli_fetch_array(mysqli_query($conn,$Sql_Query));


 if(isset($check)){

  $SuccessLoginMsg = 'Data Matched';

  // Converting the message into JSON format.
 $SuccessLoginJson = json_encode($SuccessLoginMsg);

 // Echo the message.
  echo $SuccessLoginJson ; 

  }

  else{

  // If the record inserted successfully then show the message.
 $InvalidMSG = 'Invalid Email or Password Please Try Again' ;

 // Converting the message into JSON format.
 $InvalidMSGJSon = json_encode($InvalidMSG);

 // Echo the message.
  echo $InvalidMSGJSon ;

  }
 ?>

Here's my code from the LoginScreen.js

constructor(props){
        super(props)
        this.state={
            userEmail:'',
            userPassword:''
        }
    }

    login = () =>{
    const { UserEmail }  = this.state ;
    const { UserPassword }  = this.state ;

    fetch('https://192.168.0.13:8888/login.php', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({

        user_email: UserEmail,

        user_pass: UserPassword

      })

    }).then((response) => response.json())
          .then((responseJson) => {

            // If server response message same as Data Matched
           if(responseJson === 'Data Matched')
            {

                //Then open Profile activity and send user email to profile activity.
                alert("Correct Credentials");

            }
            else{

              alert("Invalid Email or Password Please Try Again");
            }

          }).catch((error) => {
            console.error(error);
          });

      }


  render() {
    return (
      <View style={styles.container}>
        <ScrollView
          style={styles.container}
          contentContainerStyle={styles.contentContainer}>


          <View style={styles.errorContainer}>    
            <Text style={{padding:10,margin:10,color:'red'}}>{this.state.email}</Text>
          </View>


          <View style={styles.container}>
              <Text style={styles.headerText}>Username</Text>
          </View>

          <TextInput
          placeholder="Enter Email"
          style={styles.input}
          onChangeText={userEmail => this.setState({userEmail})}
          />

          <View style={styles.container}>
              <Text style={styles.headerText}>Password</Text>
          </View>

          <TextInput
          placeholder="Enter Password"
          style={styles.input}
          secureTextEntry
          onChangeText={userPassword => this.setState({userPassword})}
          />

          <TouchableOpacity style={styles.buttonContainer}
          onPress={this.login}>
          <Text style={styles.buttonText}>Login</Text>
          </TouchableOpacity>

          <View style={styles.loginContainer}>
            <Text style={styles.loginText}>Don't Have an Account?</Text>

            <TouchableOpacity style={styles.emailButtonContainer}
            onPress={() => this.props.navigation.navigate('Links')}>
              <Text style={styles.loginButtonText}>Register</Text>
            </TouchableOpacity>
          </View>


      </ScrollView>
    </View>
  );
  }
}

I'm not sure what I'm doing wrong that I'm not getting the Correct Alert response.

Any Ideas as to what I can do

L Shells
  • 21
  • 1
  • 3
  • 2
    **WARNING**: Read up on how you can use [WPDB](https://codex.wordpress.org/Class_Reference/wpdb) with prepared statements that use placeholder values like `%s` to properly escape your data. This code has some severe [SQL injection bugs](http://bobby-tables.com/) because of a lack of escaping. – tadman Apr 08 '20 at 01:25
  • 1
    Is this running in WordPress or independent of it? In either case you need to understand that the password *is not and should never be plain-text*. – tadman Apr 08 '20 at 01:26
  • Use backticks to enclose field names, not single quotes. Pls do consider the previous two comments as well. – Shadow Apr 08 '20 at 01:34

0 Answers0