Ola boa noite. Ao efetuar o login me aparece:
Falha ao buscar
O meu código login:
import React, { Component } from 'react';
import {browserHistory} from 'react-router';
export default class Login extends Component {
constructor(props){
super(props);
this.state = {msg:this.props.location.query.msg};
}
envia(event){
event.preventDefault();
const requestInfo = {
method:'POST',
body:JSON.stringify({login:this.login.value,senha:this.senha.value}),
headers:new Headers({
'Content-type' : 'application/json'
})
};
fetch('http://localhost:8080/api/public/login',requestInfo)
.then(response => {
if(response.ok) {
return response.text();
} else {
throw new Error('não foi possível fazer o login');
}
})
.then(token => {
localStorage.setItem('auth-token',token);
document.cookie = `auth-token=${token}`;
browserHistory.push('/timeline');
})
.catch(error => {
this.setState({msg:error.message});
});
}
render(){
return (
<div className="login-box">
<h1 className="header-logo">Instalura</h1>
<span>{this.state.msg}</span>
<form onSubmit={this.envia.bind(this)}>
<input type="text" ref={(input) => this.login = input}/>
<input type="password" ref={(input) => this.senha = input}/>
<input type="submit" value="login"/>
</form>
</div>
);
}
}
O código do TimelineApi
import {listagem,comentario,like,notifica} from '../actions/actionCreator';
import 'isomorphic-fetch';
export default class TimelineApi {
static lista(urlPerfil){
return dispatch => {
return fetch(urlPerfil)
.then(response => response.json())
.then(fotos => {
dispatch(listagem(fotos));
return fotos;
});
}
}
static comenta(fotoId,textoComentario) {
return dispatch => {
const requestInfo = {
method:'POST',
body:JSON.stringify({texto:textoComentario}),
headers: new Headers({
'Content-type':'application/json'
})
};
fetch(`https://instalura-api.herokuapp.com/api/fotos/${fotoId}/comment?X-AUTH-TOKEN=${localStorage.getItem('auth-token')}`,requestInfo)
.then(response => {
if(response.ok){
return response.json();
} else {
throw new Error("não foi possível comentar");
}
})
.then(novoComentario => {
dispatch(comentario(fotoId,novoComentario));
return novoComentario;
});
}
}
static like(fotoId){
return dispatch => {
fetch(`https://instalura-api.herokuapp.com/api/fotos/${fotoId}/like?X-AUTH-TOKEN=${localStorage.getItem('auth-token')}`,{method:'POST'})
.then(response => {
if(response.ok) {
return response.json();
} else {
throw new Error("não foi possível realizar o like da foto");
}
})
.then(liker => {
dispatch(like(fotoId,liker));
return liker;
});
}
}
static pesquisa(login){
return dispatch => {
fetch(`https://instalura-api.herokuapp.com/api/public/fotos/${login}`)
.then(response => response.json())
.then(fotos => {
if(fotos.length === 0){
dispatch(notifica('usuario não encontrado'));
} else {
dispatch(notifica('usuario encontrado'));
}
dispatch(listagem(fotos));
return fotos;
});
}
}
}