Refresh the page under more general conditions

This commit is contained in:
Alex Gleason 2021-03-25 15:59:09 -05:00
parent 6ead42b06d
commit 659cee1c49
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 30 additions and 11 deletions

View file

@ -124,17 +124,36 @@ const reducer = (state, action) => {
} }
}; };
const maybeReload = (oldState, state, action) => { // The user has a token stored in their browser
if (action.type === SWITCH_ACCOUNT) { const canAuth = state => {
if (location.pathname === '/auth/sign_in') { state = maybeShiftMe(state);
location.replace('/'); const me = state.get('me');
} else { const token = state.getIn(['users', me, 'access_token']);
location.reload(); return typeof token === 'string';
} };
}
if (action.type === VERIFY_CREDENTIALS_FAIL && state.get('me') !== oldState.get('me')) { // Reload, but redirect home if the user is already logged in
location.reload(); 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);
} }
}; };

View file

@ -109,7 +109,7 @@ const appReducer = combineReducers({
// Clear the state (mostly) when the user logs out // Clear the state (mostly) when the user logs out
const logOut = (state = ImmutableMap()) => { const logOut = (state = ImmutableMap()) => {
const whitelist = ['instance', 'soapbox', 'custom_emojis']; const whitelist = ['instance', 'soapbox', 'custom_emojis', 'auth'];
return ImmutableMap( return ImmutableMap(
whitelist.reduce((acc, curr) => { whitelist.reduce((acc, curr) => {