diff --git a/app/soapbox/actions/external_auth.js b/app/soapbox/actions/external_auth.js index 194b27cb1..d486b4026 100644 --- a/app/soapbox/actions/external_auth.js +++ b/app/soapbox/actions/external_auth.js @@ -20,7 +20,7 @@ const fetchExternalInstance = baseURL => { .get('/api/v1/instance') .then(({ data: instance }) => fromJS(instance)) .catch(error => { - if (error.response.status === 401) { + if (error.response && error.response.status === 401) { // Authenticated fetch is enabled. // Continue with a limited featureset. return ImmutableMap({ version: '0.0.0' }); diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index 30f02d89a..8a74a90e2 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -261,6 +261,14 @@ const persistAuthAccount = account => { } }; +const deleteForbiddenToken = (state, error, token) => { + if (error.response && [401, 403].includes(error.response.status)) { + return deleteToken(state, token); + } else { + return state; + } +}; + const reducer = (state, action) => { switch(action.type) { case AUTH_APP_CREATED: @@ -275,7 +283,7 @@ const reducer = (state, action) => { persistAuthAccount(action.account); return importCredentials(state, action.token, action.account); case VERIFY_CREDENTIALS_FAIL: - return [401, 403].includes(action.error.response.status) ? deleteToken(state, action.token) : state; + return deleteForbiddenToken(state, action.error, action.token); case SWITCH_ACCOUNT: return state.set('me', action.account.get('url')); case ME_FETCH_SKIP: diff --git a/app/soapbox/reducers/instance.js b/app/soapbox/reducers/instance.js index 5866791f2..ff94b5123 100644 --- a/app/soapbox/reducers/instance.js +++ b/app/soapbox/reducers/instance.js @@ -110,6 +110,14 @@ const persistInstance = instance => { } }; +const handleInstanceFetchFail = (state, error) => { + if (error.response && error.response.status === 401) { + return handleAuthFetch(state); + } else { + return state; + } +}; + export default function instance(state = initialState, action) { switch(action.type) { case PLEROMA_PRELOAD_IMPORT: @@ -120,7 +128,7 @@ export default function instance(state = initialState, action) { persistInstance(action.instance); return importInstance(state, fromJS(action.instance)); case INSTANCE_FETCH_FAIL: - return action.error.response.status === 401 ? handleAuthFetch(state) : state; + return handleInstanceFetchFail(state, action.error); case NODEINFO_FETCH_SUCCESS: return importNodeinfo(state, fromJS(action.nodeinfo)); case ADMIN_CONFIG_UPDATE_REQUEST: diff --git a/app/soapbox/reducers/me.js b/app/soapbox/reducers/me.js index ac6a23402..021855a14 100644 --- a/app/soapbox/reducers/me.js +++ b/app/soapbox/reducers/me.js @@ -12,6 +12,14 @@ import { const initialState = null; +const handleForbidden = (state, error) => { + if (error.response && [401, 403].includes(error.response.status)) { + return false; + } else { + return state; + } +}; + export default function me(state = initialState, action) { switch(action.type) { case ME_FETCH_SUCCESS: @@ -24,7 +32,7 @@ export default function me(state = initialState, action) { case AUTH_LOGGED_OUT: return false; case ME_FETCH_FAIL: - return [401, 403].includes(action.error.response.status) ? false : state; + return handleForbidden(state, action.error); default: return state; }