2016-06-05 01:52:18 -07:00
|
|
|
import React from 'react';
|
2018-07-17 18:09:59 -04:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import styled from 'react-emotion';
|
2018-07-17 19:13:52 -04:00
|
|
|
import { NetlifyAuthenticator } from 'netlify-cms-lib-auth';
|
2018-07-23 12:14:53 -04:00
|
|
|
import { AuthenticationPage, Icon, buttons, shadows } from 'netlify-cms-ui-default';
|
2018-07-17 18:09:59 -04:00
|
|
|
|
2018-07-23 12:14:53 -04:00
|
|
|
const LoginButtonIcon = styled(Icon)`
|
|
|
|
margin-right: 18px;
|
2018-07-17 18:09:59 -04:00
|
|
|
`
|
|
|
|
|
2018-07-23 12:14:53 -04:00
|
|
|
export default class GitHubAuthenticationPage extends React.Component {
|
2016-06-05 01:52:18 -07:00
|
|
|
static propTypes = {
|
2017-09-09 19:39:10 -06:00
|
|
|
onLogin: PropTypes.func.isRequired,
|
2017-12-07 15:37:42 -05:00
|
|
|
inProgress: PropTypes.bool,
|
2018-04-24 21:35:56 -04:00
|
|
|
base_url: PropTypes.string,
|
|
|
|
siteId: PropTypes.string,
|
|
|
|
authEndpoint: PropTypes.string,
|
2016-06-05 01:52:18 -07:00
|
|
|
};
|
|
|
|
|
2016-10-03 14:25:27 +02:00
|
|
|
state = {};
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-11-01 14:35:20 +01:00
|
|
|
handleLogin = (e) => {
|
2016-06-05 01:52:18 -07:00
|
|
|
e.preventDefault();
|
2017-05-07 06:38:33 +02:00
|
|
|
const cfg = {
|
|
|
|
base_url: this.props.base_url,
|
2018-04-24 21:35:56 -04:00
|
|
|
site_id: (document.location.host.split(':')[0] === 'localhost') ? 'cms.netlify.com' : this.props.siteId,
|
|
|
|
auth_endpoint: this.props.authEndpoint,
|
2017-08-01 20:28:03 -07:00
|
|
|
};
|
2018-07-17 19:13:52 -04:00
|
|
|
const auth = new NetlifyAuthenticator(cfg);
|
2016-06-05 01:52:18 -07:00
|
|
|
|
2016-09-13 15:30:58 +02:00
|
|
|
auth.authenticate({ provider: 'github', scope: 'repo' }, (err, data) => {
|
2016-06-05 01:52:18 -07:00
|
|
|
if (err) {
|
2016-09-13 15:30:58 +02:00
|
|
|
this.setState({ loginError: err.toString() });
|
2016-06-05 01:52:18 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onLogin(data);
|
|
|
|
});
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-06-05 01:52:18 -07:00
|
|
|
|
|
|
|
render() {
|
2017-08-31 13:46:02 -06:00
|
|
|
const { inProgress } = this.props;
|
2016-11-01 14:35:20 +01:00
|
|
|
return (
|
2018-07-23 12:14:53 -04:00
|
|
|
<AuthenticationPage
|
|
|
|
onLogin={this.handleLogin}
|
|
|
|
loginDisabled={inProgress}
|
|
|
|
loginErrorMessage={this.state.loginError}
|
|
|
|
renderButtonContent={() => (
|
|
|
|
<React.Fragment>
|
|
|
|
<LoginButtonIcon type="github"/> {inProgress ? "Logging in..." : "Login with GitHub"}
|
|
|
|
</React.Fragment>
|
|
|
|
)}
|
|
|
|
/>
|
2016-11-01 14:35:20 +01:00
|
|
|
);
|
2016-06-05 01:52:18 -07:00
|
|
|
}
|
|
|
|
}
|