1
0
mirror of https://github.com/franjsco/tick3t synced 2025-06-05 22:19:18 +02:00

improvement: various

This commit is contained in:
Francesco Esposito
2019-07-25 09:50:22 +02:00
parent 957b6c7537
commit d8a990af71

View File

@@ -9,7 +9,7 @@ import {
Input Input
} from 'reactstrap'; } from 'reactstrap';
import { config } from '../../config'; import { login } from '../../api/authentication';
import Card from '../../components/Card'; import Card from '../../components/Card';
import Button from '../../components/Button'; import Button from '../../components/Button';
@@ -44,48 +44,26 @@ class Login extends Component {
const { email, password } = this.state; const { email, password } = this.state;
const body = { login(email, password)
email,
password
}
fetch(`${config.baseURL}users/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
.then(res => {
return res.json();
})
.then((json) => { .then((json) => {
if (!json.success) { if (!json.success) {
this.setState({ this.setState({
error: json.message error: json.message
}); });
} else { } else {
localStorage.setItem('token', json.token); localStorage.setItem('user', JSON.stringify(json.data));
this.setState({ this.setState({
success: true success: true
}); });
} }
}) })
.catch((err) => { .catch((err) => {
if (err === 'AuthErr'){
this.setState({ this.setState({
err: 'auth error' error: err.message
}); });
} else {
this.setState({
err: err.message
}); });
} }
});
}
resetLogin(event) { resetLogin(event) {
event.preventDefault(); event.preventDefault();
@@ -97,10 +75,11 @@ class Login extends Component {
} }
render() { render() {
if (this.state.error) { if (this.state.error) {
return ( return (
<Card title="Error"> <Card
title="Error"
>
<p>{this.state.error}</p> <p>{this.state.error}</p>
<a href="" onClick={this.resetLogin}>Back to login</a> <a href="" onClick={this.resetLogin}>Back to login</a>
</Card> </Card>