Auth: refactor app actions
This commit is contained in:
parent
1540d61ad7
commit
a1cbbfcb02
|
@ -0,0 +1,35 @@
|
|||
import { baseClient } from '../api';
|
||||
|
||||
export const APP_CREATE_REQUEST = 'APP_CREATE_REQUEST';
|
||||
export const APP_CREATE_SUCCESS = 'APP_CREATE_SUCCESS';
|
||||
export const APP_CREATE_FAIL = 'APP_CREATE_FAIL';
|
||||
|
||||
export const APP_VERIFY_CREDENTIALS_REQUEST = 'APP_VERIFY_CREDENTIALS_REQUEST';
|
||||
export const APP_VERIFY_CREDENTIALS_SUCCESS = 'APP_VERIFY_CREDENTIALS_SUCCESS';
|
||||
export const APP_VERIFY_CREDENTIALS_FAIL = 'APP_VERIFY_CREDENTIALS_FAIL';
|
||||
|
||||
export function createApp(params) {
|
||||
return (dispatch, getState) => {
|
||||
dispatch({ type: APP_CREATE_REQUEST, params });
|
||||
return baseClient().post('/api/v1/apps', params).then(({ data: app }) => {
|
||||
dispatch({ type: APP_CREATE_SUCCESS, params, app });
|
||||
return app;
|
||||
}).catch(error => {
|
||||
dispatch({ type: APP_CREATE_FAIL, params, error });
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function verifyAppCredentials(token) {
|
||||
return (dispatch, getState) => {
|
||||
dispatch({ type: APP_VERIFY_CREDENTIALS_REQUEST, token });
|
||||
return baseClient(token).get('/api/v1/apps/verify_credentials').then(({ data: app }) => {
|
||||
dispatch({ type: APP_VERIFY_CREDENTIALS_SUCCESS, token, app });
|
||||
return app;
|
||||
}).catch(error => {
|
||||
dispatch({ type: APP_VERIFY_CREDENTIALS_FAIL, token, error });
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
}
|
|
@ -5,6 +5,7 @@ import snackbar from 'soapbox/actions/snackbar';
|
|||
import { createAccount } from 'soapbox/actions/accounts';
|
||||
import { fetchMeSuccess, fetchMeFail } from 'soapbox/actions/me';
|
||||
import { getLoggedInAccount } from 'soapbox/utils/auth';
|
||||
import { createApp } from 'soapbox/actions/apps';
|
||||
|
||||
export const SWITCH_ACCOUNT = 'SWITCH_ACCOUNT';
|
||||
|
||||
|
@ -50,7 +51,7 @@ const noOp = () => () => new Promise(f => f());
|
|||
|
||||
function createAppAndToken() {
|
||||
return (dispatch, getState) => {
|
||||
return dispatch(createApp()).then(() => {
|
||||
return dispatch(createAuthApp()).then(() => {
|
||||
return dispatch(createAppToken());
|
||||
});
|
||||
};
|
||||
|
@ -61,14 +62,16 @@ const appName = () => {
|
|||
return `SoapboxFE_${timestamp}`; // TODO: Add commit hash
|
||||
};
|
||||
|
||||
function createApp() {
|
||||
function createAuthApp() {
|
||||
return (dispatch, getState) => {
|
||||
return api(getState, 'app').post('/api/v1/apps', {
|
||||
const params = {
|
||||
client_name: appName(),
|
||||
redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
scopes: 'read write follow push admin',
|
||||
}).then(response => {
|
||||
return dispatch(authAppCreated(response.data));
|
||||
};
|
||||
|
||||
return dispatch(createApp(params)).then(app => {
|
||||
return dispatch({ type: AUTH_APP_CREATED, app });
|
||||
});
|
||||
};
|
||||
}
|
||||
|
@ -318,13 +321,6 @@ export function revokeOAuthToken(id) {
|
|||
};
|
||||
}
|
||||
|
||||
export function authAppCreated(app) {
|
||||
return {
|
||||
type: AUTH_APP_CREATED,
|
||||
app,
|
||||
};
|
||||
}
|
||||
|
||||
export function authAppAuthorized(app) {
|
||||
return {
|
||||
type: AUTH_APP_AUTHORIZED,
|
||||
|
|
Loading…
Reference in New Issue