static-cms/src/backends/netlify-auth/AuthenticationPage.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-12-23 16:59:48 -02:00
import React from "react";
import Input from "react-toolbox/lib/input";
import Button from "react-toolbox/lib/button";
import { Card, Icon } from "../../components/UI";
import logo from "./netlify_logo.svg";
import styles from "./AuthenticationPage.css";
export default class AuthenticationPage extends React.Component {
static propTypes = {
onLogin: React.PropTypes.func.isRequired,
};
2016-12-27 16:58:02 -02:00
state = { username: "", password: "", errors: {} };
2016-12-23 16:59:48 -02:00
handleChange = (name, value) => {
this.setState({ ...this.state, [name]: value });
};
handleLogin = (e) => {
e.preventDefault();
2016-12-27 16:58:02 -02:00
const { username, password } = this.state;
const errors = {};
if (!username) {
errors.username = 'Make sure to enter your user name';
}
if (!password) {
errors.password = 'Please enter your password';
}
if (Object.keys(errors).length > 0) {
this.setState({ errors });
return;
}
2016-12-23 16:59:48 -02:00
AuthenticationPage.authClient.login(this.state.username, this.state.password, true)
.then((user) => {
this.props.onLogin(user);
})
2016-12-27 16:58:02 -02:00
.catch((error) => {
this.setState({ errors: { server: error.description || error.msg || error }, loggingIn: false });
});
2016-12-23 16:59:48 -02:00
};
render() {
2016-12-27 16:58:02 -02:00
const { errors } = this.state;
2016-12-23 16:59:48 -02:00
return (
<section className={styles.root}>
<Card className={styles.card}>
2016-12-27 16:58:02 -02:00
<img src={logo} width={100} role="presentation" />
{errors.server && <p>
<span className={styles.errorMsg}>{errors.server}</span>
</p>}
<Input
type="text"
label="Username"
name="username"
value={this.state.username}
error={errors.username}
onChange={this.handleChange.bind(this, "username")} // eslint-disable-line
/>
<Input
type="password"
label="Password"
name="password"
value={this.state.password}
error={errors.password}
onChange={this.handleChange.bind(this, "password")} // eslint-disable-line
/>
2016-12-23 16:59:48 -02:00
<Button
className={styles.button}
raised
onClick={this.handleLogin}
>
<Icon type="login" /> Login
</Button>
</Card>
</section>
);
}
}