59 lines
1.5 KiB
JavaScript
Raw Normal View History

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-28 14:33:42 -06:00
import { AuthenticationPage, Icon } 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 {
static propTypes = {
onLogin: PropTypes.func.isRequired,
inProgress: PropTypes.bool,
2018-04-24 21:35:56 -04:00
base_url: PropTypes.string,
siteId: PropTypes.string,
authEndpoint: PropTypes.string,
};
2016-10-03 14:25:27 +02:00
state = {};
handleLogin = e => {
e.preventDefault();
2017-05-07 06:38:33 +02:00
const cfg = {
base_url: this.props.base_url,
site_id:
document.location.host.split(':')[0] === 'localhost'
? 'cms.netlify.com'
: this.props.siteId,
2018-04-24 21:35:56 -04:00
auth_endpoint: this.props.authEndpoint,
};
2018-07-17 19:13:52 -04:00
const auth = new NetlifyAuthenticator(cfg);
auth.authenticate({ provider: 'github', scope: 'repo' }, (err, data) => {
if (err) {
this.setState({ loginError: err.toString() });
return;
}
this.props.onLogin(data);
});
2016-10-03 14:25:27 +02:00
};
render() {
const { inProgress } = this.props;
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'}
2018-07-23 12:14:53 -04:00
</React.Fragment>
)}
/>
);
}
}