Skip to content

Authentication improvements - OAuth Login #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion client/modules/IDE/components/ErrorModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ class ErrorModal extends React.Component {
);
}

oauthError() {
const { t, service } = this.props;
const serviceLabels = {
github: 'GitHub',
google: 'Google'
};
return (
<p>
{t('ErrorModal.LinkMessage', { serviceauth: serviceLabels[service] })}
</p>
);
}

staleSession() {
return (
<p>
Expand Down Expand Up @@ -42,6 +55,8 @@ class ErrorModal extends React.Component {
return this.staleSession();
} else if (this.props.type === 'staleProject') {
return this.staleProject();
} else if (this.props.type === 'oauthError') {
return this.oauthError();
}
})()}
</div>
Expand All @@ -52,7 +67,12 @@ class ErrorModal extends React.Component {
ErrorModal.propTypes = {
type: PropTypes.string.isRequired,
closeModal: PropTypes.func.isRequired,
t: PropTypes.func.isRequired
t: PropTypes.func.isRequired,
service: PropTypes.string
};

ErrorModal.defaultProps = {
service: ''
};

export default withTranslation()(ErrorModal);
17 changes: 17 additions & 0 deletions client/modules/User/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,20 @@ export function removeApiKey(keyId) {
Promise.reject(new Error(response.data.error));
});
}

export function unlinkService(service) {
return (dispatch) => {
if (!['github', 'google'].includes(service)) return;
apiClient.delete(`/auth/${service}`)
.then((response) => {
dispatch({
type: ActionTypes.AUTH_USER,
user: response.data
});
}).catch((error) => {
const { response } = error;
const message = response.message || response.data.error;
dispatch(authError(message));
});
};
}
2 changes: 1 addition & 1 deletion client/modules/User/components/AccountForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ AccountForm.propTypes = {
newPassword: PropTypes.object.isRequired, // eslint-disable-line
}).isRequired,
user: PropTypes.shape({
verified: PropTypes.number.isRequired,
verified: PropTypes.string.isRequired,
emailVerificationInitiate: PropTypes.bool.isRequired,
}).isRequired,
handleSubmit: PropTypes.func.isRequired,
Expand Down
53 changes: 45 additions & 8 deletions client/modules/User/components/SocialAuthButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import PropTypes from 'prop-types';
import React from 'react';
import styled from 'styled-components';
import { withTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';

import { remSize } from '../../../theme';

import { GithubIcon, GoogleIcon } from '../../../common/icons';
import Button from '../../../common/Button';
import { unlinkService } from '../actions';

const authUrls = {
github: '/auth/github',
Expand All @@ -23,22 +24,51 @@ const services = {
google: 'google'
};

const servicesLabels = {
github: 'GitHub',
google: 'Google'
};

const StyledButton = styled(Button)`
width: ${remSize(300)};
`;

function SocialAuthButton({ service, t }) {
function SocialAuthButton({
service, linkStyle, isConnected, t
}) {
const ServiceIcon = icons[service];
const labels = {
github: t('SocialAuthButton.Github'),
google: t('SocialAuthButton.Google')
};
const serviceLabel = servicesLabels[service];
const loginLabel = t('SocialAuthButton.Login', { serviceauth: serviceLabel });
const connectLabel = t('SocialAuthButton.Connect', { serviceauth: serviceLabel });
const unlinkLabel = t('SocialAuthButton.Unlink', { serviceauth: serviceLabel });
const ariaLabel = t('SocialAuthButton.LogoARIA', { serviceauth: service });
const dispatch = useDispatch();
if (linkStyle) {
if (isConnected) {
return (
<StyledButton
iconBefore={<ServiceIcon aria-label={ariaLabel} />}
onClick={() => { dispatch(unlinkService(service)); }}
>
{unlinkLabel}
</StyledButton>
);
}
return (
<StyledButton
iconBefore={<ServiceIcon aria-label={ariaLabel} />}
href={authUrls[service]}
>
{connectLabel}
</StyledButton>
);
}
return (
<StyledButton
iconBefore={<ServiceIcon aria-label={t('SocialAuthButton.LogoARIA', { serviceauth: service })} />}
iconBefore={<ServiceIcon aria-label={ariaLabel} />}
href={authUrls[service]}
>
{labels[service]}
{loginLabel}
</StyledButton>
);
}
Expand All @@ -47,9 +77,16 @@ SocialAuthButton.services = services;

SocialAuthButton.propTypes = {
service: PropTypes.oneOf(['github', 'google']).isRequired,
linkStyle: PropTypes.bool,
isConnected: PropTypes.bool,
t: PropTypes.func.isRequired
};

SocialAuthButton.defaultProps = {
linkStyle: false,
isConnected: false
};

const SocialAuthButtonPublic = withTranslation()(SocialAuthButton);
SocialAuthButtonPublic.services = services;
export default SocialAuthButtonPublic;
52 changes: 47 additions & 5 deletions client/modules/User/pages/AccountView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { bindActionCreators } from 'redux';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import { Helmet } from 'react-helmet';
import { withTranslation } from 'react-i18next';
import { withRouter, browserHistory } from 'react-router';
import { parse } from 'query-string';
import { updateSettings, initiateVerification, createApiKey, removeApiKey } from '../actions';
import AccountForm from '../components/AccountForm';
import apiClient from '../../../utils/apiClient';
import { validateSettings } from '../../../utils/reduxFormUtils';
import SocialAuthButton from '../components/SocialAuthButton';
import APIKeyForm from '../components/APIKeyForm';
import Nav from '../../../components/Nav';
import ErrorModal from '../../IDE/components/ErrorModal';
import Overlay from '../../App/components/Overlay';

function SocialLoginPanel(props) {
const { user } = props;
return (
<React.Fragment>
<AccountForm {...props} />
Expand All @@ -24,19 +29,37 @@ function SocialLoginPanel(props) {
{props.t('AccountView.SocialLoginDescription')}
</p>
<div className="account__social-stack">
<SocialAuthButton service={SocialAuthButton.services.github} />
<SocialAuthButton service={SocialAuthButton.services.google} />
<SocialAuthButton
service={SocialAuthButton.services.github}
linkStyle
isConnected={!!user.github}
/>
<SocialAuthButton
service={SocialAuthButton.services.google}
linkStyle
isConnected={!!user.google}
/>
</div>
</React.Fragment>
);
}

SocialLoginPanel.propTypes = {
user: PropTypes.shape({
github: PropTypes.string,
google: PropTypes.string
}).isRequired
};

class AccountView extends React.Component {
componentDidMount() {
document.body.className = this.props.theme;
}

render() {
const queryParams = parse(this.props.location.search);
const showError = !!queryParams.error;
const errorType = queryParams.error;
const accessTokensUIEnabled = window.process.env.UI_ACCESS_TOKEN_ENABLED;

return (
Expand All @@ -47,6 +70,21 @@ class AccountView extends React.Component {

<Nav layout="dashboard" />

{showError &&
<Overlay
title={this.props.t('ErrorModal.LinkTitle')}
ariaLabel={this.props.t('ErrorModal.LinkTitle')}
closeOverlay={() => {
browserHistory.push(this.props.location.pathname);
}}
>
<ErrorModal
type="oauthError"
service={errorType}
/>
</Overlay>
}

<main className="account-settings">
<header className="account-settings__header">
<h1 className="account-settings__title">{this.props.t('AccountView.Settings')}</h1>
Expand Down Expand Up @@ -111,13 +149,17 @@ function asyncValidate(formProps, dispatch, props) {
AccountView.propTypes = {
previousPath: PropTypes.string.isRequired,
theme: PropTypes.string.isRequired,
t: PropTypes.func.isRequired
t: PropTypes.func.isRequired,
location: PropTypes.shape({
search: PropTypes.string.isRequired,
pathname: PropTypes.string.isRequired
}).isRequired
};

export default withTranslation()(reduxForm({
export default withTranslation()(withRouter(reduxForm({
form: 'updateAllSettings',
fields: ['username', 'email', 'currentPassword', 'newPassword'],
validate: validateSettings,
asyncValidate,
asyncBlurFields: ['username', 'email', 'currentPassword']
}, mapStateToProps, mapDispatchToProps)(AccountView));
}, mapStateToProps, mapDispatchToProps)(AccountView)));
6 changes: 5 additions & 1 deletion client/styles/components/_error-modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

.error-modal__content {
padding: #{20 / $base-font-size}rem;
padding-top: 0;
padding-top: #{40 / $base-font-size}rem;
padding-bottom: #{60 / $base-font-size}rem;
max-width: #{500 / $base-font-size}rem;
& p {
font-size: #{16 / $base-font-size}rem;
}
}
46 changes: 41 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"primer-tooltips": "^1.5.11",
"prop-types": "^15.6.2",
"q": "^1.4.1",
"query-string": "^6.13.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^5.1.3",
Expand Down
Loading