2018-06-11 19:03:43 -07:00
|
|
|
import React from 'react';
|
2018-07-17 19:13:52 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2018-08-27 10:23:21 -06:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2018-07-23 12:14:53 -04:00
|
|
|
import styled from 'react-emotion';
|
2018-07-17 19:13:52 -04:00
|
|
|
import { NetlifyAuthenticator, ImplicitAuthenticator } from 'netlify-cms-lib-auth';
|
2018-07-25 06:56:53 -04:00
|
|
|
import { AuthenticationPage, Icon } from 'netlify-cms-ui-default';
|
2018-06-11 19:03:43 -07:00
|
|
|
|
2018-07-23 12:14:53 -04:00
|
|
|
const LoginButtonIcon = styled(Icon)`
|
|
|
|
margin-right: 18px;
|
2018-08-07 14:46:54 -06:00
|
|
|
`;
|
2018-07-23 12:14:53 -04:00
|
|
|
|
|
|
|
export default class GitLabAuthenticationPage extends React.Component {
|
2018-06-11 19:03:43 -07:00
|
|
|
static propTypes = {
|
|
|
|
onLogin: PropTypes.func.isRequired,
|
|
|
|
inProgress: PropTypes.bool,
|
2018-08-27 10:23:21 -06:00
|
|
|
base_url: PropTypes.string,
|
|
|
|
siteId: PropTypes.string,
|
|
|
|
authEndpoint: PropTypes.string,
|
|
|
|
config: ImmutablePropTypes.map,
|
|
|
|
clearHash: PropTypes.function,
|
2018-06-11 19:03:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const authType = this.props.config.getIn(['backend', 'auth_type']);
|
2018-08-07 14:46:54 -06:00
|
|
|
if (authType === 'implicit') {
|
2018-06-11 19:03:43 -07:00
|
|
|
this.auth = new ImplicitAuthenticator({
|
2018-08-07 14:46:54 -06:00
|
|
|
base_url: this.props.config.getIn(['backend', 'base_url'], 'https://gitlab.com'),
|
2018-06-11 19:03:43 -07:00
|
|
|
auth_endpoint: this.props.config.getIn(['backend', 'auth_endpoint'], 'oauth/authorize'),
|
2018-06-14 08:54:53 -06:00
|
|
|
app_id: this.props.config.getIn(['backend', 'app_id']),
|
2018-07-03 15:47:15 -04:00
|
|
|
clearHash: this.props.clearHash,
|
2018-06-11 19:03:43 -07:00
|
|
|
});
|
|
|
|
// Complete implicit authentication if we were redirected back to from the provider.
|
|
|
|
this.auth.completeAuth((err, data) => {
|
|
|
|
if (err) {
|
|
|
|
this.setState({ loginError: err.toString() });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onLogin(data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.auth = new NetlifyAuthenticator({
|
|
|
|
base_url: this.props.base_url,
|
2018-08-07 14:46:54 -06:00
|
|
|
site_id:
|
|
|
|
document.location.host.split(':')[0] === 'localhost'
|
|
|
|
? 'cms.netlify.com'
|
|
|
|
: this.props.siteId,
|
2018-06-11 19:03:43 -07:00
|
|
|
auth_endpoint: this.props.authEndpoint,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
handleLogin = e => {
|
2018-06-11 19:03:43 -07:00
|
|
|
e.preventDefault();
|
|
|
|
this.auth.authenticate({ provider: 'gitlab', scope: 'api' }, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
this.setState({ loginError: err.toString() });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onLogin(data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2018-11-02 18:25:36 +01:00
|
|
|
const { inProgress, config } = this.props;
|
2018-06-11 19:03:43 -07:00
|
|
|
return (
|
2018-07-23 12:14:53 -04:00
|
|
|
<AuthenticationPage
|
|
|
|
onLogin={this.handleLogin}
|
|
|
|
loginDisabled={inProgress}
|
|
|
|
loginErrorMessage={this.state.loginError}
|
2018-11-02 18:25:36 +01:00
|
|
|
logoUrl={config.get('logo_url')}
|
2018-07-23 12:14:53 -04:00
|
|
|
renderButtonContent={() => (
|
|
|
|
<React.Fragment>
|
2018-08-07 14:46:54 -06:00
|
|
|
<LoginButtonIcon type="gitlab" /> {inProgress ? 'Logging in...' : 'Login with GitLab'}
|
2018-07-23 12:14:53 -04:00
|
|
|
</React.Fragment>
|
|
|
|
)}
|
|
|
|
/>
|
2018-06-11 19:03:43 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|