Clean up auth code
This commit is contained in:
parent
0feff2833c
commit
30384cbfef
|
@ -1,5 +1,5 @@
|
||||||
import api from '../api';
|
import api from '../api';
|
||||||
import { showAlert, showAlertForError } from 'soapbox/actions/alerts';
|
import { showAlert } from 'soapbox/actions/alerts';
|
||||||
import { fetchMe } from 'soapbox/actions/me';
|
import { fetchMe } from 'soapbox/actions/me';
|
||||||
|
|
||||||
export const AUTH_APP_CREATED = 'AUTH_APP_CREATED';
|
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_SUCCESS = 'RESET_PASSWORD_SUCCESS';
|
||||||
export const RESET_PASSWORD_FAIL = 'RESET_PASSWORD_FAIL';
|
export const RESET_PASSWORD_FAIL = 'RESET_PASSWORD_FAIL';
|
||||||
|
|
||||||
const hasAppToken = getState => getState().hasIn(['auth', 'app', 'access_token']);
|
|
||||||
const noOp = () => () => new Promise(f => f());
|
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() {
|
function createAppAndToken() {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
return dispatch(createApp()).then(() => {
|
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) {
|
function createUserToken(username, password) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const app = getState().getIn(['auth', 'app']);
|
const app = getState().getIn(['auth', 'app']);
|
||||||
|
@ -133,7 +113,7 @@ export function logOut() {
|
||||||
export function register(params) {
|
export function register(params) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
dispatch({ type: AUTH_REGISTER_REQUEST });
|
dispatch({ type: AUTH_REGISTER_REQUEST });
|
||||||
return dispatch(initAuthApp()).then(() => {
|
return dispatch(createAppAndToken()).then(() => {
|
||||||
return api(getState, 'app').post('/api/v1/accounts', params);
|
return api(getState, 'app').post('/api/v1/accounts', params);
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
dispatch({ type: AUTH_REGISTER_SUCCESS, token: response.data });
|
dispatch({ type: AUTH_REGISTER_SUCCESS, token: response.data });
|
||||||
|
|
Loading…
Reference in New Issue