如何在异步本地响应中存储acces令牌

时间:2018-12-19 13:20:37

标签: php react-native

数据使用acces令牌存储在db中,但是在react控制台中,错误显示SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符 我想我丢失了一些存储访问令牌

const ACCESS_TOKEN ='accesstoken'; 
export default class Project extends Component {     
constructor() {
    super()
    this.state = {
      UserName: '',
      UserPhonenumber: '',
      UserPassword: ''
    }
  }
  async storeToken(accessToken) {
    try {
        await AsyncStorage.setItem(ACCESS_TOKEN, accesstoken);
        console.log("Token was stored successfull ");
    } catch(error) {
        console.log("Something went wrong");
    }
}
UserRegistrationFunction = () =>{
    const {UserName} = this.state;
    const {UserPhonenumber} = this.state;
    const {UserPassword} = this.state;

 if(UserName=="" || UserPhonenumber=="" || UserPassword==""){
      alert("Please fill out all fields");
    } else{
    fetch('http://192.168.63.1/test/user_registration.php', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      name: this.state.UserName,
      phonenumber: this.state.UserPhonenumber,
      password: this.state.UserPassword
    })
  }).then((response) => response.json())
        .then((responseJson) => {             
  // Showing response message coming from server after inserting records.
          Alert.alert(responseJson);
        }).catch((error) => {
          console.error(error);
        });
    }
}

这是我的php后端

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
 $json = file_get_contents('php://input');

// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
 $name = $obj['name'];
 $phonenumber = $obj['phonenumber'];
 $password = $obj['password'];
$password = hash('sha256', $password);
$accesstoken = $obj['accesstoken'];
$accesstoken = md5(uniqid(rand(), true));

$CheckSQL = "SELECT * FROM user_details WHERE phonenumber='$phonenumber'";

// Executing SQL Query.
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));

$CheckSQL1 = "SELECT * FROM user_details WHERE name='$name'";
$check1 = mysqli_fetch_array(mysqli_query($con,$CheckSQL1));
if(isset($check)){
 $PhonenumberExistMSG = 'Number Already Exist, Please Try Again !!!';
 $PhonenumberExistJson = json_encode($PhonenumberExistMSG);
// Echo the message.
 echo $PhonenumberExistJson ; 
 }elseif(isset($check1)){
    $NameExistMSG = 'Name Already Exist, Please Try Again !!!';
 // Converting the message into JSON format.
$NameExistJson = json_encode($NameExistMSG);

// Echo the message.
 echo $NameExistJson ; 
 }
 else{
 $Sql_Query = "insert into user_details (name,phonenumber,password,accesstoken) values ('$name','$phonenumber','$password','$accesstoken')";
 if(mysqli_query($con,$Sql_Query)){
 $MSG = 'User Registered Successfully' ;
 $json = json_encode($MSG);
  echo $json ;
 }
 else{
 echo 'Try Again';
 }
 }
 mysqli_close($con);

accesstoken存储在db bt中,而不是在控制台中存储。如何解决它,我是本机反应的新手

0 个答案:

没有答案