Olá pessoal! Usei a API na url: 'https://instalura-api.herokuapp.com/api/public/login' para fazer o login porém não me retorna o token. Fiz testes com console.log e notei que as states retornam os dados do inputs corretamente: {usuario:"rafael",senha:"123456"} Creio que minha função efetuarLogin() também está correta pois testando em outras urls de APIS vem o retorno correto. A única explicação que encontrei é que o problema dever ser na API da alura. O erro warning é "Possible Unhandled Promise Rejection (id: 0); Error: Não foi possível efetuar o login." Obrigado pela atenção, aguardo retorno.
import React, { Component } from 'react'
import { View, Dimensions, StyleSheet, TextInput, Button } from 'react-native'
const dimensions = Dimensions.get('screen');
export default class Login extends Component {
efetuaLogin(){
let uri = 'https://instalura-api.herokuapp.com/api/public/login'; console.log(this.state)
const requestInfo = {
method:'POST',
body:JSON.stringify({
login:this.state.usuario,
senha:this.state.senha
}),
header: new Headers({
'Content-Type':'application/json'
}),
}
fetch(uri).then(r1=>{ //console.log(r1);
if(r1.ok) return r1.text();
throw new Error("Não foi possível efetuar o login.");
}).then(r2=>console.warn(r2));
}
render() {
return (
<View>
<View style={styles.form}>
<TextInput style={styles.input} placeholder="Usuário..."
onChangeText={texto=>this.setState({usuario:texto})} autoCapitalize="none" />
<TextInput style={styles.input} placeholder="Senha..."
onChangeText={texto=>this.setState({senha:texto})} secureTextEntry={true} />
<Button title="Login" onPress={this.efetuaLogin.bind(this)}
color='darkgreen' />
</View>
</View>
);
}
}
const styles = StyleSheet.create({
form:{
width:dimensions.width * 0.8,
},
input:{
height:40,
borderBottomWidth: 1,
borderBottomColor:'#ddd'
}
});