1

My login code was working properly when I was using react-native-0.41.2 and react-15.4.2. Now I have upgraded react-native to 0.52 and react 0.16.2. But now API is not giving any response when I tried to login into the app. I also tried to make some changes in Info.plist file keys but it didn't work. Please provide me any suggestion for it.

enter image description here

class Login extends React.Component {
  login(e) {
    e.preventDefault();
    var user = '';
    var pass = '';
    user = this.state.username;
    pass = this.state.password;
    this.authenticate(user, pass);
    return;
  }
  async authenticate(username, password) {
    let _this = this;
    Api.login(username, password)
    .then(async response => {await response.json()})
    .then(async response => {
      //My code
    });
 }
 render() {
  let { username, password } = this.state;
  return (
       <View >
        <TextField
          label='username'
          value={username}
          autoCapitalize= 'none'
          onChangeText={ (username) => this.setState({ username }) }
        />
        <TextField
          label='password'
          value={password}
          secureTextEntry autoCapitalize= 'none'
          onChangeText={ (password) => this.setState({ password }) }
          style={{marginBottom: 10}}
        />
        <TouchableOpacity style={styles.loginButtons} onPress={(e) => 
        this.login(e)}>
          <Text style={styles.buttonText}> Login </Text>
        </TouchableOpacity>

      </View>
   );
 }
} 

My api code -

login(username, password) {
let url = '/api/sessions';
return fetch(BASE_URL + url, {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    user: {
      email: username,
      password: password
    }
  })
});}
Samiksha Jagtap
  • 585
  • 2
  • 13
  • 29
  • It might be cause of you using **fetch** For good idea, you should post the source code or provide some clue it will easily for everyone to answer straight to your question dear. – Seyha Jan 18 '18 at 06:48

1 Answers1

0

Ok, let using Catch to catch the execption in your request. Seem your code OK, let try to find out the problem in catch and make sure your request URL is in correct path request.

For example:

fetch('... Your URL')
    .then((response) => response.json())
    .then((responseJson) => {
      return responseJson.movies;
    })
    .catch((error) => {
      console.error(error);
    });

Hope it will help for some idea...

Seyha
  • 323
  • 4
  • 12