bigbuffet-rw/app/soapbox/actions/mfa.js

85 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-08-07 13:17:13 -07:00
import api from '../api';
2022-01-07 12:26:19 -08:00
export const MFA_FETCH_REQUEST = 'MFA_FETCH_REQUEST';
export const MFA_FETCH_SUCCESS = 'MFA_FETCH_SUCCESS';
export const MFA_FETCH_FAIL = 'MFA_FETCH_FAIL';
2020-08-07 13:17:13 -07:00
2022-01-07 12:26:19 -08:00
export const MFA_BACKUP_CODES_FETCH_REQUEST = 'MFA_BACKUP_CODES_FETCH_REQUEST';
export const MFA_BACKUP_CODES_FETCH_SUCCESS = 'MFA_BACKUP_CODES_FETCH_SUCCESS';
export const MFA_BACKUP_CODES_FETCH_FAIL = 'MFA_BACKUP_CODES_FETCH_FAIL';
2020-08-07 13:17:13 -07:00
2022-01-07 12:26:19 -08:00
export const MFA_SETUP_REQUEST = 'MFA_SETUP_REQUEST';
export const MFA_SETUP_SUCCESS = 'MFA_SETUP_SUCCESS';
export const MFA_SETUP_FAIL = 'MFA_SETUP_FAIL';
2020-08-07 13:17:13 -07:00
2022-01-07 12:26:19 -08:00
export const MFA_CONFIRM_REQUEST = 'MFA_CONFIRM_REQUEST';
export const MFA_CONFIRM_SUCCESS = 'MFA_CONFIRM_SUCCESS';
export const MFA_CONFIRM_FAIL = 'MFA_CONFIRM_FAIL';
2020-08-07 13:17:13 -07:00
2022-01-07 12:26:19 -08:00
export const MFA_DISABLE_REQUEST = 'MFA_DISABLE_REQUEST';
export const MFA_DISABLE_SUCCESS = 'MFA_DISABLE_SUCCESS';
export const MFA_DISABLE_FAIL = 'MFA_DISABLE_FAIL';
2020-08-07 13:17:13 -07:00
2022-01-07 12:26:19 -08:00
export function fetchMfa() {
2020-08-07 13:17:13 -07:00
return (dispatch, getState) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_FETCH_REQUEST });
return api(getState).get('/api/pleroma/accounts/mfa').then(({ data }) => {
dispatch({ type: MFA_FETCH_SUCCESS, data });
2020-08-07 13:17:13 -07:00
}).catch(error => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_FETCH_FAIL });
2020-08-07 13:17:13 -07:00
});
};
}
export function fetchBackupCodes() {
return (dispatch, getState) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_BACKUP_CODES_FETCH_REQUEST });
return api(getState).get('/api/pleroma/accounts/mfa/backup_codes').then(({ data }) => {
dispatch({ type: MFA_BACKUP_CODES_FETCH_SUCCESS, data });
return data;
2020-08-07 13:17:13 -07:00
}).catch(error => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_BACKUP_CODES_FETCH_FAIL });
2020-08-07 13:17:13 -07:00
});
};
}
2022-01-07 12:26:19 -08:00
export function setupMfa(method) {
2020-08-07 13:17:13 -07:00
return (dispatch, getState) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_SETUP_REQUEST, method });
return api(getState).get(`/api/pleroma/accounts/mfa/setup/${method}`).then(({ data }) => {
dispatch({ type: MFA_SETUP_SUCCESS, data });
return data;
2020-08-07 13:17:13 -07:00
}).catch(error => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_SETUP_FAIL });
throw error;
2020-08-07 13:17:13 -07:00
});
};
}
2022-01-07 12:26:19 -08:00
export function confirmMfa(method, code, password) {
2020-08-07 13:17:13 -07:00
return (dispatch, getState) => {
2022-01-07 12:26:19 -08:00
const params = { code, password };
dispatch({ type: MFA_CONFIRM_REQUEST, method, code });
return api(getState).post(`/api/pleroma/accounts/mfa/confirm/${method}`, params).then(({ data }) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_CONFIRM_SUCCESS, method, code });
return data;
2020-08-07 13:17:13 -07:00
}).catch(error => {
dispatch({ type: MFA_CONFIRM_FAIL, method, code, error, skipAlert: true });
throw error;
2020-08-07 13:17:13 -07:00
});
};
}
2022-01-07 12:26:19 -08:00
export function disableMfa(method, password) {
2020-08-07 13:17:13 -07:00
return (dispatch, getState) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_DISABLE_REQUEST, method });
return api(getState).delete(`/api/pleroma/accounts/mfa/${method}`, { data: { password } }).then(({ data }) => {
2022-01-07 12:26:19 -08:00
dispatch({ type: MFA_DISABLE_SUCCESS, method });
return data;
2020-08-07 13:17:13 -07:00
}).catch(error => {
dispatch({ type: MFA_DISABLE_FAIL, method, skipAlert: true });
throw error;
2020-08-07 13:17:13 -07:00
});
};
}