0
I have a login page, which authenticates by token and soon after, with the user and password entered are returned the user data. Soon after that I redirect the user to a component called Dashboard. However I wanted to pass the data of it to this component.
Index.js Here I have my routes created
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import Sobre from './Sobre.js';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import Dashboard from './ui/Dashboard';
import LoginForm from './ui/LoginForm';
ReactDOM.render(
<BrowserRouter>
<Switch>
<Route path="/" exact={true} component={LoginForm} />
<Route path="/sobre" component={Sobre} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</BrowserRouter>
,document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.register();
Login page
In the isUser function it is redirected to Dashboard. There is one condition left if it is user or not yet, but this is not the case.
import React, { Component } from 'react';
export default class LoginForm extends Component{
constructor(){
super();
this.state = {
login: '',
senha: '',
authenticate_code: '',
url: 'http://192.168.0.19/api/'
};
this.handleLogin = this.handleLogin.bind(this);
this.isUser = this.isUser.bind(this);
}
handleLogin(e){
e.preventDefault();
let dataToGetToken= {
nome: "teste",
senha: "1234"
};
try{
fetch(this.state.url + 'token', {
method: 'POST',
body: JSON.stringify(dataToGetToken),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => {
this.setState({ authenticate_code: 'bearer ' + json.token })
this.isUser(this.refs.login.value, this.refs.pass.value, this.state.authenticate_code);
});
}
catch(e){
console.log(e);
}
}
isUser(name, pass, token){
var login = new Object();
login.nome = name;
login.senha = pass;
console.log(login);
console.log(token);
try{
fetch(this.state.url + 'usuarios/getuser', {
method: 'POST',
body: JSON.stringify(login),
headers: {
"Authorization": token,
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => {
//Redireciona
this.props.history.push('/ui/Dashboard')
});
}
catch(e){
console.log(e);
}
}
render(){
return(
<div className="App">
<div className="container">
<div className="row">
<div className="col-sm-12">
<form onSubmit={this.handleLogin}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Login</label>
<input type="text" ref="login" className="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"/>
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Senha</label>
<input type="password" ref="pass" className="form-control" id="exampleInputPassword1" placeholder="Password" />
</div>
<div className="form-group form-check">
<input type="checkbox" className="form-check-input" id="exampleCheck1" />
<label className="form-check-label" htmlFor="exampleCheck1">Check me out</label>
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
);
}
}
I want to redirect to this component but I want to get the data returned from login
import React, { Component } from 'react';
export default class Dashboard extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div></div>
);
}
}