From 377c0fb64a25f7a2cf714919914594804053608c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 7 May 2021 21:46:08 -0500 Subject: [PATCH 1/3] Only delete token if it 403's --- app/soapbox/reducers/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index 5c920f1c8..5985939da 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -155,7 +155,7 @@ const reducer = (state, action) => { case VERIFY_CREDENTIALS_SUCCESS: return importCredentials(state, action.token, action.account); case VERIFY_CREDENTIALS_FAIL: - return deleteToken(state, action.token); + return action.error.response.status === 403 ? deleteToken(state, action.token) : state; case SWITCH_ACCOUNT: return state.set('me', action.accountId); default: From 9a859dad14cbf3df22a5154c36e4513a3d73185c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 7 May 2021 21:49:08 -0500 Subject: [PATCH 2/3] Properly dispatch fetchMeFail, don't show white screen on VERIFY_CREDENTIALS_FAIL --- app/soapbox/actions/auth.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/soapbox/actions/auth.js b/app/soapbox/actions/auth.js index 80956ec6f..1475f9753 100644 --- a/app/soapbox/actions/auth.js +++ b/app/soapbox/actions/auth.js @@ -2,7 +2,7 @@ import api, { baseClient } from '../api'; import { importFetchedAccount } from './importer'; import snackbar from 'soapbox/actions/snackbar'; import { createAccount } from 'soapbox/actions/accounts'; -import { fetchMeSuccess } from 'soapbox/actions/me'; +import { fetchMeSuccess, fetchMeFail } from 'soapbox/actions/me'; export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT'; @@ -136,14 +136,16 @@ export function otpVerify(code, mfa_token) { export function verifyCredentials(token) { return (dispatch, getState) => { + const me = getState().get('me'); dispatch({ type: VERIFY_CREDENTIALS_REQUEST }); return baseClient(token).get('/api/v1/accounts/verify_credentials').then(({ data: account }) => { dispatch(importFetchedAccount(account)); dispatch({ type: VERIFY_CREDENTIALS_SUCCESS, token, account }); - if (account.id === getState().get('me')) dispatch(fetchMeSuccess(account)); + if (account.id === me) dispatch(fetchMeSuccess(account)); return account; }).catch(error => { + if (me === null) dispatch(fetchMeFail(error)); dispatch({ type: VERIFY_CREDENTIALS_FAIL, token, error }); }); }; From 0586ec426487f4ff6d83acf78b27a575d2c96b95 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 7 May 2021 22:43:44 -0500 Subject: [PATCH 3/3] Fix auth reducer tests --- app/soapbox/reducers/__tests__/auth-test.js | 23 +++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/app/soapbox/reducers/__tests__/auth-test.js b/app/soapbox/reducers/__tests__/auth-test.js index 103cfc4b0..a149042c4 100644 --- a/app/soapbox/reducers/__tests__/auth-test.js +++ b/app/soapbox/reducers/__tests__/auth-test.js @@ -179,7 +179,7 @@ describe('auth reducer', () => { }); describe('VERIFY_CREDENTIALS_FAIL', () => { - it('should delete the failed token', () => { + it('should delete the failed token if it 403\'d', () => { const state = fromJS({ tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' }, @@ -191,7 +191,12 @@ describe('auth reducer', () => { 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, }); - const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const action = { + type: VERIFY_CREDENTIALS_FAIL, + token: 'ABCDEFG', + error: { response: { status: 403 } }, + }; + const result = reducer(state, action); expect(result.get('tokens')).toEqual(expected); }); @@ -208,7 +213,12 @@ describe('auth reducer', () => { '5678': { id: '5678', access_token: 'HIJKLMN' }, }); - const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const action = { + type: VERIFY_CREDENTIALS_FAIL, + token: 'ABCDEFG', + error: { response: { status: 403 } }, + }; + const result = reducer(state, action); expect(result.get('users')).toEqual(expected); }); @@ -222,7 +232,12 @@ describe('auth reducer', () => { }, }); - const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const action = { + type: VERIFY_CREDENTIALS_FAIL, + token: 'ABCDEFG', + error: { response: { status: 403 } }, + }; + const result = reducer(state, action); expect(result.get('me')).toEqual('5678'); });