From 6ead42b06d832fd3b922c140c1ab81fcb76ee784 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 25 Mar 2021 15:15:37 -0500 Subject: [PATCH] Handle logout --- app/soapbox/actions/auth.js | 13 ++++++------- .../features/ui/components/profile_dropdown.js | 6 ------ app/soapbox/reducers/auth.js | 14 +++++++++++--- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/soapbox/actions/auth.js b/app/soapbox/actions/auth.js index 6b74c6234..be18746a9 100644 --- a/app/soapbox/actions/auth.js +++ b/app/soapbox/actions/auth.js @@ -177,17 +177,16 @@ export function logIn(username, password) { export function logOut() { return (dispatch, getState) => { const state = getState(); + const me = state.getIn(['auth', 'me']); - dispatch({ type: AUTH_LOGGED_OUT }); - - // Attempt to destroy OAuth token on logout - api(getState).post('/oauth/revoke', { + return api(getState).post('/oauth/revoke', { client_id: state.getIn(['auth', 'app', 'client_id']), client_secret: state.getIn(['auth', 'app', 'client_secret']), - token: state.getIn(['auth', 'user', 'access_token']), + token: state.getIn(['auth', 'users', me, 'access_token']), + }).finally(() => { + dispatch({ type: AUTH_LOGGED_OUT, accountId: me }); + dispatch(snackbar.success('Logged out.')); }); - - dispatch(snackbar.success('Logged out.')); }; } diff --git a/app/soapbox/features/ui/components/profile_dropdown.js b/app/soapbox/features/ui/components/profile_dropdown.js index 34311ca17..7d644591a 100644 --- a/app/soapbox/features/ui/components/profile_dropdown.js +++ b/app/soapbox/features/ui/components/profile_dropdown.js @@ -1,6 +1,5 @@ import React from 'react'; import { connect } from 'react-redux'; -import { openModal } from '../../../actions/modal'; import { fetchOwnAccounts } from 'soapbox/actions/auth'; import { throttle } from 'lodash'; import PropTypes from 'prop-types'; @@ -65,11 +64,6 @@ class ProfileDropdown extends React.PureComponent { }; } - handleAddAccount = e => { - this.props.dispatch(openModal('LOGIN')); - e.preventDefault(); - } - fetchOwnAccounts = throttle(() => { this.props.dispatch(fetchOwnAccounts()); }, 2000); diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index d44767f87..8b5b2e0fc 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -47,7 +47,7 @@ const maybeShiftMe = state => { } }; -const importFailedToken = (state, token) => { +const deleteToken = (state, token) => { return state.withMutations(state => { state.update('tokens', ImmutableMap(), tokens => tokens.delete(token)); state.update('users', ImmutableMap(), users => users.filterNot(user => user.get('access_token') === token)); @@ -55,6 +55,14 @@ const importFailedToken = (state, token) => { }); }; +const deleteUser = (state, accountId) => { + return state.withMutations(state => { + state.update('users', ImmutableMap(), users => users.delete(accountId)); + state.update('tokens', ImmutableMap(), tokens => tokens.filterNot(token => token.get('account') === accountId)); + maybeShiftMe(state); + }); +}; + // Upgrade the initial state const migrateLegacy = state => { if (localState) return state; @@ -104,11 +112,11 @@ const reducer = (state, action) => { case AUTH_LOGGED_IN: return importToken(state, action.token); case AUTH_LOGGED_OUT: - return state.set('user', ImmutableMap()); + return deleteUser(state, action.accountId); case VERIFY_CREDENTIALS_SUCCESS: return importCredentials(state, action.token, action.account); case VERIFY_CREDENTIALS_FAIL: - return importFailedToken(state, action.token); + return deleteToken(state, action.token); case SWITCH_ACCOUNT: return state.set('me', action.accountId); default: