Always check error.response before error.response.status
This commit is contained in:
parent
5a19d52617
commit
e42030daae
4 changed files with 28 additions and 4 deletions
|
@ -20,7 +20,7 @@ const fetchExternalInstance = baseURL => {
|
||||||
.get('/api/v1/instance')
|
.get('/api/v1/instance')
|
||||||
.then(({ data: instance }) => fromJS(instance))
|
.then(({ data: instance }) => fromJS(instance))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.response.status === 401) {
|
if (error.response && error.response.status === 401) {
|
||||||
// Authenticated fetch is enabled.
|
// Authenticated fetch is enabled.
|
||||||
// Continue with a limited featureset.
|
// Continue with a limited featureset.
|
||||||
return ImmutableMap({ version: '0.0.0' });
|
return ImmutableMap({ version: '0.0.0' });
|
||||||
|
|
|
@ -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) => {
|
const reducer = (state, action) => {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case AUTH_APP_CREATED:
|
case AUTH_APP_CREATED:
|
||||||
|
@ -275,7 +283,7 @@ const reducer = (state, action) => {
|
||||||
persistAuthAccount(action.account);
|
persistAuthAccount(action.account);
|
||||||
return importCredentials(state, action.token, action.account);
|
return importCredentials(state, action.token, action.account);
|
||||||
case VERIFY_CREDENTIALS_FAIL:
|
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:
|
case SWITCH_ACCOUNT:
|
||||||
return state.set('me', action.account.get('url'));
|
return state.set('me', action.account.get('url'));
|
||||||
case ME_FETCH_SKIP:
|
case ME_FETCH_SKIP:
|
||||||
|
|
|
@ -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) {
|
export default function instance(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case PLEROMA_PRELOAD_IMPORT:
|
case PLEROMA_PRELOAD_IMPORT:
|
||||||
|
@ -120,7 +128,7 @@ export default function instance(state = initialState, action) {
|
||||||
persistInstance(action.instance);
|
persistInstance(action.instance);
|
||||||
return importInstance(state, fromJS(action.instance));
|
return importInstance(state, fromJS(action.instance));
|
||||||
case INSTANCE_FETCH_FAIL:
|
case INSTANCE_FETCH_FAIL:
|
||||||
return action.error.response.status === 401 ? handleAuthFetch(state) : state;
|
return handleInstanceFetchFail(state, action.error);
|
||||||
case NODEINFO_FETCH_SUCCESS:
|
case NODEINFO_FETCH_SUCCESS:
|
||||||
return importNodeinfo(state, fromJS(action.nodeinfo));
|
return importNodeinfo(state, fromJS(action.nodeinfo));
|
||||||
case ADMIN_CONFIG_UPDATE_REQUEST:
|
case ADMIN_CONFIG_UPDATE_REQUEST:
|
||||||
|
|
|
@ -12,6 +12,14 @@ import {
|
||||||
|
|
||||||
const initialState = null;
|
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) {
|
export default function me(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ME_FETCH_SUCCESS:
|
case ME_FETCH_SUCCESS:
|
||||||
|
@ -24,7 +32,7 @@ export default function me(state = initialState, action) {
|
||||||
case AUTH_LOGGED_OUT:
|
case AUTH_LOGGED_OUT:
|
||||||
return false;
|
return false;
|
||||||
case ME_FETCH_FAIL:
|
case ME_FETCH_FAIL:
|
||||||
return [401, 403].includes(action.error.response.status) ? false : state;
|
return handleForbidden(state, action.error);
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue