Clean up auth code

This commit is contained in:
Alex Gleason 2020-05-29 09:55:38 -05:00
parent 0feff2833c
commit 30384cbfef
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,5 +1,5 @@
import api from '../api';
import { showAlert, showAlertForError } from 'soapbox/actions/alerts';
import { showAlert } from 'soapbox/actions/alerts';
import { fetchMe } from 'soapbox/actions/me';
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
@ -15,20 +15,8 @@ export const RESET_PASSWORD_REQUEST = 'RESET_PASSWORD_REQUEST';
export const RESET_PASSWORD_SUCCESS = 'RESET_PASSWORD_SUCCESS';
export const RESET_PASSWORD_FAIL = 'RESET_PASSWORD_FAIL';
const hasAppToken = getState => getState().hasIn(['auth', 'app', 'access_token']);
const noOp = () => () => new Promise(f => f());
function initAuthApp() {
return (dispatch, getState) => {
const hasToken = hasAppToken(getState);
const action = hasToken ? verifyApp : createAppAndToken;
return dispatch(action())
.catch(error => {
dispatch(showAlertForError(error));
});
};
}
function createAppAndToken() {
return (dispatch, getState) => {
return dispatch(createApp()).then(() => {
@ -69,14 +57,6 @@ function createAppToken() {
};
}
function verifyApp() {
return (dispatch, getState) => {
return api(getState, 'app').get('/api/v1/apps/verify_credentials')
.then (response => dispatch(authAppCreated(response.data)))
.catch(error => dispatch(createAppAndToken()));
};
}
function createUserToken(username, password) {
return (dispatch, getState) => {
const app = getState().getIn(['auth', 'app']);
@ -133,7 +113,7 @@ export function logOut() {
export function register(params) {
return (dispatch, getState) => {
dispatch({ type: AUTH_REGISTER_REQUEST });
return dispatch(initAuthApp()).then(() => {
return dispatch(createAppAndToken()).then(() => {
return api(getState, 'app').post('/api/v1/accounts', params);
}).then(response => {
dispatch({ type: AUTH_REGISTER_SUCCESS, token: response.data });