From 659cee1c491653eadfdf2eaf85a9279fd14d21c2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 25 Mar 2021 15:59:09 -0500 Subject: [PATCH] Refresh the page under more general conditions --- app/soapbox/reducers/auth.js | 39 ++++++++++++++++++++++++++--------- app/soapbox/reducers/index.js | 2 +- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index 8b5b2e0fc..a6d1d58cb 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -124,17 +124,36 @@ const reducer = (state, action) => { } }; -const maybeReload = (oldState, state, action) => { - if (action.type === SWITCH_ACCOUNT) { - if (location.pathname === '/auth/sign_in') { - location.replace('/'); - } else { - location.reload(); - } - } +// The user has a token stored in their browser +const canAuth = state => { + state = maybeShiftMe(state); + const me = state.get('me'); + const token = state.getIn(['users', me, 'access_token']); + return typeof token === 'string'; +}; - if (action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me')) { - location.reload(); +// Reload, but redirect home if the user is already logged in +const reload = state => { + if (location.pathname === '/auth/sign_in' && canAuth(state)) { + return location.replace('/'); + } else { + return location.reload(); + } +}; + +// `me` has changed from one valid ID to another +const userSwitched = (oldState, state) => { + const validMe = state => typeof state.get('me') === 'string'; + + const stillValid = validMe(oldState) && validMe(state); + const didChange = oldState.get('me') !== state.get('me'); + + return stillValid && didChange; +}; + +const maybeReload = (oldState, state, action) => { + if (userSwitched(oldState, state)) { + reload(state); } }; diff --git a/app/soapbox/reducers/index.js b/app/soapbox/reducers/index.js index 116766fab..48ddd77b8 100644 --- a/app/soapbox/reducers/index.js +++ b/app/soapbox/reducers/index.js @@ -109,7 +109,7 @@ const appReducer = combineReducers({ // Clear the state (mostly) when the user logs out const logOut = (state = ImmutableMap()) => { - const whitelist = ['instance', 'soapbox', 'custom_emojis']; + const whitelist = ['instance', 'soapbox', 'custom_emojis', 'auth']; return ImmutableMap( whitelist.reduce((acc, curr) => {