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 {
|
2017-09-05 14:07:56 -07:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-05 19:30:03 -07:00
|
|
|
this.identity = window.netlifyIdentity;
|
2017-09-05 14:07:56 -07:00
|
|
|
this.state = {user: this.identity && this.identity.gotrue && this.identity.gotrue.currentUser()};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.identity && !this.state.user) {
|
|
|
|
this.identity.on('login', (user) => {
|
|
|
|
this.props.onLogin(user);
|
|
|
|
this.identity.close();
|
|
|
|
});
|
|
|
|
this.identity.on('signup', (user) => {
|
|
|
|
this.props.onLogin(user);
|
|
|
|
this.identity.close();
|
|
|
|
});
|
|
|
|
this.identity.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
static propTypes = {
|
|
|
|
onLogin: React.PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-09-05 14:07:56 -07:00
|
|
|
state = { email: "", 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
|
|
|
|
2017-09-05 14:07:56 -07:00
|
|
|
const { email, password } = this.state;
|
2016-12-27 16:58:02 -02:00
|
|
|
const errors = {};
|
2017-09-05 14:07:56 -07:00
|
|
|
if (!email) {
|
|
|
|
errors.email = 'Make sure to enter your user name';
|
2016-12-27 16:58:02 -02:00
|
|
|
}
|
|
|
|
if (!password) {
|
|
|
|
errors.password = 'Please enter your password';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(errors).length > 0) {
|
|
|
|
this.setState({ errors });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:07:56 -07:00
|
|
|
AuthenticationPage.authClient.login(this.state.email, this.state.password, true)
|
2016-12-23 16:59:48 -02:00
|
|
|
.then((user) => {
|
|
|
|
this.props.onLogin(user);
|
|
|
|
})
|
2017-09-04 23:24:57 -07:00
|
|
|
.catch((error) => {
|
2016-12-27 16:58:02 -02:00
|
|
|
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;
|
2017-01-26 19:23:42 -02:00
|
|
|
const { error } = this.props;
|
2017-09-05 14:07:56 -07:00
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
return (
|
|
|
|
<section className={styles.root}>
|
|
|
|
<Card className={styles.card}>
|
2017-09-04 23:24:57 -07:00
|
|
|
<form onSubmit={this.handleLogin}>
|
|
|
|
<img src={logo} width={100} role="presentation" />
|
|
|
|
{error && <p>
|
|
|
|
<span className={styles.errorMsg}>{error}</span>
|
|
|
|
</p>}
|
|
|
|
{errors.server && <p>
|
|
|
|
<span className={styles.errorMsg}>{errors.server}</span>
|
|
|
|
</p>}
|
|
|
|
<Input
|
|
|
|
type="text"
|
2017-09-05 14:07:56 -07:00
|
|
|
label="Email"
|
|
|
|
name="email"
|
|
|
|
value={this.state.email}
|
|
|
|
error={errors.email}
|
|
|
|
onChange={this.handleChange.bind(this, "email")} // eslint-disable-line
|
2017-09-04 23:24:57 -07:00
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
type="password"
|
|
|
|
label="Password"
|
|
|
|
name="password"
|
|
|
|
value={this.state.password}
|
|
|
|
error={errors.password}
|
|
|
|
onChange={this.handleChange.bind(this, "password")} // eslint-disable-line
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
className={styles.button}
|
|
|
|
raised
|
|
|
|
>
|
|
|
|
<Icon type="login" /> Login
|
|
|
|
</Button>
|
|
|
|
</form>
|
2016-12-23 16:59:48 -02:00
|
|
|
</Card>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|