I'm trying to create a login in react-native
. For that I send the user info to the webservice. if the info is correct the server will return a json
The problem is that I send the correct user info to the server and I get an error message as if I put the incorrect info
But when I test with Postman
with the same info I dont get any error message
constructor
constructor() {
super()
this.state = {
showPass: true,
press: false,
username: "",
password: ""
}
}
function that fetch the data
checkLogin = () => {
const { username, password } = this.state;
var url = 'https://xxxxxxx/xxxx/xxxx/xxxx/auth/login'
fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username, password
})
})
.then((response) => response.json())
.then((responseJson) => {
if (!responseJson.errors) {
this.props.navigation.navigate('foto');
console.log(username + " 1");
console.log(password + " 1");
} else {
Alert.alert('Error', responseJson.message, [{
text: 'Fechar'
}]);
console.log(username + " 2");
console.log(password + " 2");
}
})
.catch((error) => {
console.error(error);
})
Inputs
<TextInput onChangeText={text => this.setState({ username: text })} />
<TextInput onChangeText={text => this.setState({ password: text })} />
<TouchableOpacity style={Style.btnLogin} onPress={() => this.checkLogin()} >
<Text style={Style.textoLogin}>Entrar</Text>
</TouchableOpacity>
JSON that I get from Postman
with the same info from the input
{
"errors": false,
"type": "-",
"line": "-",
"message": "Login successfully!",
"user": {
"user_id": 2,
"name": "xxxxxxx",
"username": "xxxxxxxx",
"email": "xxxxx@xxxxx.pt"
},
"_token": "xxxxxxxxxxx"
}