Skip to content
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

(improvements) Notifications and auth flow when users try to re-add the existing account #860

Merged
Merged
2 changes: 2 additions & 0 deletions public/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"apiSecret": "API Secret",
"accWithApiKey": "Add account with API key",
"accWithoutApiKey": "Add account without API key",
"accAddedWithApiKey": "You have already added this account using an API key",
"accAddedWithApiKeyLogin": "You have already added this account using an API key, logging you in...",
"simpleAccounts": "Simple Accounts",
"multipleAccounts": "Multiple Accounts",
"login": "Login",
Expand Down
4 changes: 4 additions & 0 deletions src/components/Auth/SignUp/SignUp.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { connect } from 'react-redux'
import { withTranslation } from 'react-i18next'

import {
signIn,
signUp,
signUpOtp,
updateAuth,
signUpEmail,
showOtpLogin,
} from 'state/auth/actions'
import { updateStatus } from 'state/status/actions'
import {
getUsers,
getAuthData,
Expand All @@ -26,11 +28,13 @@ const mapStateToProps = state => ({
})

const mapDispatchToProps = {
signIn,
signUp,
signUpOtp,
updateAuth,
signUpEmail,
showOtpLogin,
updateStatus,
}

export default compose(
Expand Down
32 changes: 32 additions & 0 deletions src/components/Auth/SignUp/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
Dialog,
Intent,
} from '@blueprintjs/core'
import _map from 'lodash/map'
import _find from 'lodash/find'
import _isEqual from 'lodash/isEqual'
import _includes from 'lodash/includes'

import Icon from 'icons'
import config from 'config'
Expand All @@ -34,12 +38,14 @@ class SignUp extends PureComponent {
}).isRequired,
loading: PropTypes.bool.isRequired,
t: PropTypes.func.isRequired,
signIn: PropTypes.func.isRequired,
signUp: PropTypes.func.isRequired,
signUpOtp: PropTypes.func.isRequired,
signUpEmail: PropTypes.func.isRequired,
showOtpLogin: PropTypes.func.isRequired,
isOtpLoginShown: PropTypes.bool.isRequired,
switchMode: PropTypes.func.isRequired,
updateStatus: PropTypes.func.isRequired,
users: PropTypes.arrayOf(PropTypes.shape({
email: PropTypes.string.isRequired,
isSubAccount: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -85,6 +91,7 @@ class SignUp extends PureComponent {
})
tracker.trackEvent('Add Account')
const isValid = this.validateForm()
const isRegisteredUserName = this.validateUsername(userName)
if (isValid) {
if (useApiKey) {
signUp({
Expand All @@ -94,6 +101,8 @@ class SignUp extends PureComponent {
isNotProtected: !isPasswordProtected,
isPersisted,
})
} else if (isRegisteredUserName) {
this.handleExistingUser(userName)
} else {
signUpEmail({
login: userName,
Expand Down Expand Up @@ -144,6 +153,29 @@ class SignUp extends PureComponent {
return isValid
}

validateUsername = (userName) => {
const { users } = this.props
const isRegisteredUserName = _includes(
_map(users, user => user.email), userName,
)
return isRegisteredUserName
}

handleExistingUser = (userName) => {
const {
users, switchMode, updateStatus, signIn,
} = this.props
const registeredUser = _find(users, user => _isEqual(user.email, userName))
const { email, isSubAccount, isNotProtected } = registeredUser
if (isNotProtected) {
updateStatus({ id: 'auth.accAddedWithApiKeyLogin' })
signIn({ email, isSubAccount })
} else {
switchMode(MODES.SIGN_IN)
updateStatus({ id: 'auth.accAddedWithApiKey' })
}
}

handleInputChange = (e) => {
const { isBeingValidated } = this.state
const { name, value } = e.target
Expand Down
1 change: 1 addition & 0 deletions src/components/Status/_Status.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

.bp3-toast {
min-height: 40px;
max-width: 550px;
padding: 5px;
border-radius: 2px;
align-items: center;
Expand Down