From b032d04317099fd90bbb3eacd4b681759b240521 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Fri, 27 Oct 2017 23:18:08 +0200 Subject: [PATCH] :cake: User switching enhancements --- src/redux/modules/userSwitching.js | 40 ++++++++++++++++++++++-------- src/redux/store.js | 9 +++++-- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/redux/modules/userSwitching.js b/src/redux/modules/userSwitching.js index 8b2eebd40..065b8af87 100644 --- a/src/redux/modules/userSwitching.js +++ b/src/redux/modules/userSwitching.js @@ -1,20 +1,36 @@ -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); + } } }; @@ -22,13 +38,17 @@ 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 ); diff --git a/src/redux/store.js b/src/redux/store.js index f8f3029ba..481a073b0 100644 --- a/src/redux/store.js +++ b/src/redux/store.js @@ -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']); @@ -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 =>