import React, { Component } from 'react';
import {browserHistory} from 'react-router';
export default class Login extends Component {
constructor(props){
super(props);
//console.log(this.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('https://instalura-api.herokuapp.com/api/login', requestInfo)
.then(response => {
if(response.ok){
return response.text();
} else {
// this.setState({msg: 'nao foi possivel fazer o login'});
throw new Error('Nao foi possivel acessar');
}
})
.then(token => {
//console.log(token);
//console.log('teste');
localStorage.setItem('auth-token', token);
console.log(localStorage);
browserHistory.push('/timeline');
})
.catch(error => {
this.setState({msg:error.message});
// browserHistory.push('/timeline');
})
}
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>
);
}
}
Retorna
[object Storage]: {auth-token: "", authToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6ImZsYXZpbyIsImVtYWlsIjoiZmxhdmlvQGFsdXJhcGljLmNvbS5iciIsImlhdCI6MTU1OTIxNzkzMSwiZXhwIjoxNTU5MzA0MzMxfQ.UES-bHdm2hVvqt1wzv9GvmHAMyvd7mG2MyXb0B0YYnI", length: 2} auth-token: "" authToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6ImZsYXZpbyIsImVtYWlsIjoiZmxhdmlvQGFsdXJhcGljLmNvbS5iciIsImlhdCI6MTU1OTIxNzkzMSwiZXhwIjoxNTU5MzA0MzMxfQ.UES-bHdm2hVvqt1wzv9GvmHAMyvd7mG2MyXb0B0YYnI" length: 2
proto: StoragePrototype