0

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"
}

1 Answers1

0

So I solved my problem, I had to change my body to a FormData()

Here is the code:


checkLogin = () => {

        var url = 'https://xxxxx/xxxx/xxx/xxx/auth/login';
        const { username, password } = this.state;
        /*const username1 = this.state.username;
        const password1 = this.state.password;
        var data = {
            "username" : this.state.username,
            "password" : this.state.password 
        }*/
        var formData=new FormData();
        formData.append('username', username);
        formData.append('password',password);


        fetch(url, {
            method: 'POST',
           // body: JSON.stringify(data)
           body: formData
        })
        .then((response) => response.json())
        .then((responseJson) => {
            if (!responseJson.errors) {
                this.storeData(JSON.stringify(responseJson.user), responseJson._token)
                this.props.navigation.navigate('foto');

            } else {
                Alert.alert('Error', responseJson.message, [{
                    text: 'Fechar'
                }]);


            }


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