1
0
mirror of https://github.com/franjsco/tick3t synced 2025-01-01 04:07:28 +01:00

feat: implement login

This commit is contained in:
frab1t 2019-07-03 10:27:41 +02:00
parent ed94230f79
commit ee13b24293

View File

@ -1,4 +1,5 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Link, Redirect } from 'react-router-dom';
import { import {
Row, Row,
Col, Col,
@ -8,6 +9,8 @@ import {
Input Input
} from 'reactstrap'; } from 'reactstrap';
import { config } from '../../config';
import Card from '../../components/Card'; import Card from '../../components/Card';
import Button from '../../components/Button'; import Button from '../../components/Button';
@ -16,20 +19,21 @@ class Login extends Component {
super(props); super(props);
this.state = { this.state = {
email: '', email: null,
password: '', password: null,
err: '' error: null,
success: false
}; };
this.handleInputChange = this.handleInputChange.bind(this); this.handleInputChange = this.handleInputChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.resetLogin = this.resetLogin.bind(this);
} }
handleInputChange(event) { handleInputChange(event) {
const target = event.target; const target = event.target;
const name = target.name; const name = target.name;
const value = target.type === 'checkbox' ? target.checked : target.value; const value = target.type === 'checkbox' ? target.checked : target.value;
this.setState({ this.setState({
[name]: value [name]: value
}); });
@ -37,18 +41,78 @@ class Login extends Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
const { email, password } = this.state;
const body = {
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) => {
if (!json.success) {
this.setState({
error: json.message
});
} else {
localStorage.setItem('token', json.token);
this.setState({
success: true
});
}
})
.catch((err) => {
if (err === 'AuthErr'){
this.setState({
err: 'auth error'
});
} else {
this.setState({
err: err.message
});
}
});
}
resetLogin(event) {
event.preventDefault();
this.setState({
email: null,
password: null,
error: null
});
} }
render() { render() {
if (this.state.err) { if (this.state.error) {
return ( return (
<Card title="Error"> <Card title="Error">
<p>{this.state.err}</p> <p>{this.state.error}</p>
<a href="" onClick={this.resetLogin}>Back to login</a>
</Card> </Card>
); );
} }
if (this.state.success) {
return (
<Redirect to="/admin/" />
)
}
return ( return (
<Card <Card
align="left" align="left"
@ -67,31 +131,31 @@ class Login extends Component {
> >
Email Email
</Label> </Label>
<Input <Input
type="email" type="email"
name="email" name="email"
id="email" id="email"
onChange={this.handleInputChange} onChange={this.handleInputChange}
placeholder="email@email.com" placeholder="email@email.com"
/> />
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>
<Label <Label
className="font-weight-bold" className="font-weight-bold"
for="password" for="password"
> >
Password Password
</Label> </Label>
<Input <Input
onChange={this.handleInputChange} onChange={this.handleInputChange}
type="password" type="password"
name="password" name="password"
id="password" id="password"
placeholder="your password" placeholder="your password"
/> />
</FormGroup> </FormGroup>
<FormGroup> <FormGroup>
<Button>Login</Button> <Button>Login</Button>
</FormGroup> </FormGroup>