Skip to content

Commit

Permalink
Merge pull request #119 from ReCodEx/reload-page-after-takeover
Browse files Browse the repository at this point in the history
User switching enhancements
  • Loading branch information
SemaiCZE committed Oct 28, 2017
2 parents 3fdad53 + b032d04 commit e674ed1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
40 changes: 30 additions & 10 deletions src/redux/modules/userSwitching.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
import { handleActions } from 'redux-actions';
import { actionTypes } from './auth';
import { handleActions, createAction } from 'redux-actions';
import { actionTypes as authActionTypes } from './auth';
import { decode, isTokenValid } from '../helpers/token';
import { addNotification } from './notifications';

export const actionTypes = {
REMOVE_USER: 'recodex/userSwitching/REMOVE_USER'
};

export const removeUser = createAction(actionTypes.REMOVE_USER);

export const switchUser = userId => (dispatch, getState) => {
const state = getState().userSwitching;
const user = state[userId] ? state[userId] : null;
const decodedToken = decode(user.accessToken);
if (!user.accessToken || !isTokenValid(decodedToken)) {
dispatch(addNotification('The token has already expired.', false));
const { user, accessToken } = state[userId] ? state[userId] : null;
const decodedToken = decode(accessToken);
if (!accessToken || !isTokenValid(decodedToken)) {
dispatch(
addNotification(
`The token has already expired, you cannot switch to user ${user.fullName}. This account will be removed from the user switching panel.`,
false
)
);
dispatch(removeUser(userId));
} else {
dispatch({
type: actionTypes.LOGIN_SUCCESS,
payload: user,
type: authActionTypes.LOGIN_SUCCESS,
payload: { user, accessToken },
meta: { service: 'takeover' }
});

if (window && window.location && window.location.reload) {
window.location.reload(true);
}
}
};

const initialState = {};

const reducer = handleActions(
{
[actionTypes.LOGIN_SUCCESS]: (state, { payload }) =>
[authActionTypes.LOGIN_SUCCESS]: (state, { payload }) =>
state[payload.user.id]
? state
: {
...state,
[payload.user.id]: payload
}
},
[actionTypes.REMOVE_USER]: (state, { payload: userId }) =>
Object.keys(state)
.filter(id => id !== userId)
.reduce((acc, id) => ({ ...acc, [id]: state[id] }), {})
},
initialState
);
Expand Down
9 changes: 7 additions & 2 deletions src/redux/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import apiMiddleware from './middleware/apiMiddleware';
import createReducer from './reducer';
import createEngine from 'redux-storage-engine-localstorage';
import filter from 'redux-storage-decorator-filter';
import { actionTypes } from './modules/auth';
import { actionTypes as authActionTypes } from './modules/auth';
import { actionTypes as switchingActionTypes } from './modules/userSwitching';

const engine = filter(createEngine('recodex/store'), ['userSwitching']);

Expand All @@ -20,7 +21,11 @@ const getMiddleware = history => [
promiseMiddleware(),
thunkMiddleware,
routerMiddleware(history),
storage.createMiddleware(engine, [], [actionTypes.LOGIN_SUCCESS])
storage.createMiddleware(
engine,
[],
[authActionTypes.LOGIN_SUCCESS, switchingActionTypes.REMOVE_USER]
)
];

const dev = history =>
Expand Down

0 comments on commit e674ed1

Please sign in to comment.