diff --git a/src/components/accounts/AccountSwitcher.js b/src/components/accounts/AccountSwitcher.js
index 69ec944..c42093d 100644
--- a/src/components/accounts/AccountSwitcher.js
+++ b/src/components/accounts/AccountSwitcher.js
@@ -1,14 +1,14 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
-
+import { connect } from 'react-redux';
import classNames from 'classnames';
import { Link } from 'react-router-dom';
import { FormattedMessage as Message } from 'react-intl';
-
import loader from 'services/loader';
import { skins, SKIN_DARK, COLOR_WHITE } from 'components/ui';
import { Button } from 'components/ui/form';
-import { userShape } from 'components/user/User';
+import { authenticate, revoke } from 'components/accounts/actions';
+import { getActiveAccount } from 'components/accounts/reducer';
import styles from './accountSwitcher.scss';
import messages from './AccountSwitcher.intl.json';
@@ -22,7 +22,6 @@ export class AccountSwitcher extends Component {
onAfterAction: PropTypes.func, // called after each action performed
onSwitch: PropTypes.func, // called after switching an account. The active account will be passed as arg
accounts: PropTypes.object, // eslint-disable-line
- user: userShape, // TODO: remove me, when we will be sure, that accounts.active is always set for user (event after register)
skin: PropTypes.oneOf(skins),
highlightActiveAccount: PropTypes.bool, // whether active account should be expanded and shown on the top
allowLogout: PropTypes.bool, // whether to show logout icon near each account
@@ -40,8 +39,7 @@ export class AccountSwitcher extends Component {
render() {
const { accounts, skin, allowAdd, allowLogout, highlightActiveAccount } = this.props;
- // const activeAccount = accounts.active || this.props.user;
- const activeAccount = this.props.user;
+ const activeAccount = getActiveAccount({ accounts });
let {available} = accounts;
@@ -83,14 +81,14 @@ export class AccountSwitcher extends Component {
) : null}
- {available.map((account, id) => (
+ {available.map((account, index) => (
{allowLogout ? (
@@ -156,12 +154,8 @@ export class AccountSwitcher extends Component {
};
}
-import { connect } from 'react-redux';
-import { authenticate, revoke } from 'components/accounts/actions';
-
-export default connect(({accounts, user}) => ({
+export default connect(({accounts}) => ({
accounts,
- user
}), {
switchAccount: authenticate,
removeAccount: revoke
diff --git a/src/components/accounts/actions.js b/src/components/accounts/actions.js
index f646c62..cd7195a 100644
--- a/src/components/accounts/actions.js
+++ b/src/components/accounts/actions.js
@@ -9,6 +9,7 @@ import { setAccountSwitcher } from 'components/auth/actions';
import { getActiveAccount } from 'components/accounts/reducer';
import logger from 'services/logger';
+import type { Account, State as AccountsState } from './reducer';
import {
add,
remove,
@@ -16,7 +17,6 @@ import {
reset,
updateToken
} from './actions/pure-actions';
-import type { Account, State as AccountsState } from './reducer';
type Dispatch = (action: Object) => Promise<*>;
@@ -45,8 +45,19 @@ export function authenticate(account: Account | {
const {token, refreshToken} = account;
const email = account.email || null;
- return (dispatch: Dispatch, getState: () => State): Promise =>
- authentication.validateToken({token, refreshToken})
+ return (dispatch: Dispatch, getState: () => State): Promise => {
+ const accountId: number | null = typeof account.id === 'number' ? account.id : null;
+ const knownAccount: ?Account = accountId
+ ? getState().accounts.available.find((item) => item.id === accountId)
+ : null;
+
+ if (knownAccount) {
+ // this account is already available
+ // activate it before validation
+ dispatch(activate(knownAccount));
+ }
+
+ return authentication.validateToken({token, refreshToken})
.catch((resp = {}) => {
// all the logic to get the valid token was failed,
// looks like we have some problems with token
@@ -97,6 +108,7 @@ export function authenticate(account: Account | {
return dispatch(setLocale(user.lang))
.then(() => account);
});
+ };
}
/**
@@ -219,9 +231,17 @@ export function revoke(account: Account) {
if (accountToReplace) {
return dispatch(authenticate(accountToReplace))
- .then(() => {
- authentication.logout(account);
+ .finally(() => {
+ // we need to logout user, even in case, when we can
+ // not authenticate him with new account
+ authentication.logout(account)
+ .catch(() => {
+ // we don't care
+ });
dispatch(remove(account));
+ })
+ .catch(() => {
+ // we don't care
});
}
diff --git a/src/components/accounts/actions.test.js b/src/components/accounts/actions.test.js
index 14bf959..68ec1b2 100644
--- a/src/components/accounts/actions.test.js
+++ b/src/components/accounts/actions.test.js
@@ -190,6 +190,35 @@ describe('components/accounts/actions', () => {
)
);
});
+
+ describe('when one account available', () => {
+ beforeEach(() => {
+ getState.returns({
+ accounts: {
+ active: account.id,
+ available: [account]
+ },
+ auth: {
+ credentials: {},
+ },
+ user,
+ });
+ });
+
+ it('should activate account before auth api call', () => {
+ authentication.validateToken.returns(Promise.reject({ error: 'foo'}));
+
+ return expect(
+ authenticate(account)(dispatch, getState),
+ 'to be rejected with',
+ { error: 'foo'}
+ ).then(() =>
+ expect(dispatch, 'to have a call satisfying', [
+ activate(account)
+ ])
+ );
+ });
+ });
});
describe('#revoke()', () => {
diff --git a/src/components/userbar/LoggedInPanel.js b/src/components/userbar/LoggedInPanel.js
index 5dffcd9..2590722 100644
--- a/src/components/userbar/LoggedInPanel.js
+++ b/src/components/userbar/LoggedInPanel.js
@@ -1,16 +1,12 @@
// @flow
import React, { Component } from 'react';
-
import classNames from 'classnames';
-
import { AccountSwitcher } from 'components/accounts';
import styles from './loggedInPanel.scss';
-import type { User } from 'components/user';
-
export default class LoggedInPanel extends Component<{
- user: User
+ username: string
}, {
isAccountSwitcherActive: bool
}> {
@@ -38,7 +34,7 @@ export default class LoggedInPanel extends Component<{
}
render() {
- const { user } = this.props;
+ const { username } = this.props;
const { isAccountSwitcherActive } = this.state;
return (
@@ -48,7 +44,7 @@ export default class LoggedInPanel extends Component<{
})}>
diff --git a/src/components/userbar/Userbar.js b/src/components/userbar/Userbar.js
index 713a460..b170f3e 100644
--- a/src/components/userbar/Userbar.js
+++ b/src/components/userbar/Userbar.js
@@ -1,31 +1,26 @@
-import PropTypes from 'prop-types';
+// @flow
+import type { Account } from 'components/accounts/reducer';
import React, { Component } from 'react';
-
import { Link } from 'react-router-dom';
import { FormattedMessage as Message } from 'react-intl';
-
import buttons from 'components/ui/buttons.scss';
import messages from './Userbar.intl.json';
import styles from './userbar.scss';
-
-import { userShape } from 'components/user/User';
-
import LoggedInPanel from './LoggedInPanel';
-export default class Userbar extends Component {
+export default class Userbar extends Component<{
+ account: ?Account,
+ guestAction: 'register' | 'login',
+}> {
static displayName = 'Userbar';
- static propTypes = {
- user: userShape,
- guestAction: PropTypes.oneOf(['register', 'login'])
- };
static defaultProps = {
guestAction: 'register'
};
render() {
- const { user } = this.props;
+ const { account } = this.props;
let { guestAction } = this.props;
switch (guestAction) {
@@ -48,12 +43,12 @@ export default class Userbar extends Component {
return (