2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
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";
|
2017-09-06 17:58:32 -07:00
|
|
|
import { Notifs } from 'redux-notifications';
|
|
|
|
import { Toast } from '../../components/UI/index';
|
2016-12-23 16:59:48 -02:00
|
|
|
import { Card, Icon } from "../../components/UI";
|
|
|
|
import logo from "./netlify_logo.svg";
|
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
let component = null;
|
|
|
|
|
|
|
|
if (window.netlifyIdentity) {
|
|
|
|
window.netlifyIdentity.on('login', (user) => {
|
|
|
|
component && component.handleIdentityLogin(user);
|
|
|
|
});
|
|
|
|
window.netlifyIdentity.on('logout', () => {
|
|
|
|
component && component.handleIdentityLogout();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
export default class AuthenticationPage extends React.Component {
|
2017-09-05 14:07:56 -07:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-06 17:58:32 -07:00
|
|
|
component = this;
|
2017-09-05 14:07:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-09-05 19:30:03 -07:00
|
|
|
if (!this.loggedIn && window.netlifyIdentity && window.netlifyIdentity.currentUser()) {
|
|
|
|
this.props.onLogin(window.netlifyIdentity.currentUser());
|
2017-09-06 17:58:32 -07:00
|
|
|
window.netlifyIdentity.close();
|
2017-09-05 14:07:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
componentWillUnmount() {
|
|
|
|
component = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIdentityLogin = (user) => {
|
|
|
|
this.props.onLogin(user);
|
|
|
|
window.netlifyIdentity.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIdentityLogout = () => {
|
|
|
|
window.netlifyIdentity.open();
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:58:32 -07:00
|
|
|
handleIdentity = () => {
|
|
|
|
if (window.netlifyIdentity.currentUser()) {
|
|
|
|
this.props.onLogin(user);
|
|
|
|
} else {
|
|
|
|
window.netlifyIdentity.open();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
static propTypes = {
|
2017-09-09 19:39:10 -06:00
|
|
|
onLogin: PropTypes.func.isRequired,
|
2017-08-31 13:46:02 -06:00
|
|
|
inProgress: PropTypes.bool.isRequired,
|
2016-12-23 16:59:48 -02:00
|
|
|
};
|
|
|
|
|
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) {
|
2017-09-06 18:28:20 -07:00
|
|
|
errors.email = 'Make sure to enter your email.';
|
2016-12-27 16:58:02 -02:00
|
|
|
}
|
|
|
|
if (!password) {
|
2017-09-06 18:28:20 -07:00
|
|
|
errors.password = 'Please enter your password.';
|
2016-12-27 16:58:02 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-31 13:46:02 -06:00
|
|
|
const { error, inProgress } = this.props;
|
2017-09-05 14:07:56 -07:00
|
|
|
|
2017-09-05 19:30:03 -07:00
|
|
|
if (window.netlifyIdentity) {
|
2017-10-18 09:29:38 -07:00
|
|
|
return <section className="nc-gitGatewayAuthenticationPage-root">
|
2017-09-06 17:58:32 -07:00
|
|
|
<Notifs CustomComponent={Toast} />
|
2017-10-18 09:29:38 -07:00
|
|
|
<Button className="nc-gitGatewayAuthenticationPage-button" raised onClick={this.handleIdentity}>
|
2017-09-06 17:58:32 -07:00
|
|
|
Login with Netlify Identity
|
|
|
|
</Button>
|
|
|
|
</section>
|
2017-09-05 19:30:03 -07:00
|
|
|
}
|
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
return (
|
2017-10-18 09:29:38 -07:00
|
|
|
<section className="nc-gitGatewayAuthenticationPage-root">
|
|
|
|
<Card className="nc-gitGatewayAuthenticationPage-card">
|
2017-09-04 23:24:57 -07:00
|
|
|
<form onSubmit={this.handleLogin}>
|
|
|
|
<img src={logo} width={100} role="presentation" />
|
|
|
|
{error && <p>
|
2017-10-18 09:29:38 -07:00
|
|
|
<span className="nc-gitGatewayAuthenticationPage-errorMsg">{error}</span>
|
2017-09-04 23:24:57 -07:00
|
|
|
</p>}
|
|
|
|
{errors.server && <p>
|
2017-10-18 09:29:38 -07:00
|
|
|
<span className="nc-gitGatewayAuthenticationPage-errorMsg">{errors.server}</span>
|
2017-09-04 23:24:57 -07:00
|
|
|
</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
|
2017-10-18 09:29:38 -07:00
|
|
|
className="nc-gitGatewayAuthenticationPage-button"
|
2017-09-04 23:24:57 -07:00
|
|
|
raised
|
2017-08-31 13:46:02 -06:00
|
|
|
disabled={inProgress}
|
2017-09-04 23:24:57 -07:00
|
|
|
>
|
2017-08-31 13:46:02 -06:00
|
|
|
<Icon type="login" /> {inProgress ? "Logging in..." : "Login"}
|
2017-09-04 23:24:57 -07:00
|
|
|
</Button>
|
|
|
|
</form>
|
2016-12-23 16:59:48 -02:00
|
|
|
</Card>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|