From a1cbbfcb02c3a2f4db3b23e39706b14997d4b802 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 21 Aug 2021 19:05:59 -0500 Subject: [PATCH] Auth: refactor app actions --- app/soapbox/actions/apps.js | 35 +++++++++++++++++++++++++++++++++++ app/soapbox/actions/auth.js | 20 ++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 app/soapbox/actions/apps.js diff --git a/app/soapbox/actions/apps.js b/app/soapbox/actions/apps.js new file mode 100644 index 000000000..11262427b --- /dev/null +++ b/app/soapbox/actions/apps.js @@ -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; + }); + }; +} diff --git a/app/soapbox/actions/auth.js b/app/soapbox/actions/auth.js index c0c112c24..b9a315eec 100644 --- a/app/soapbox/actions/auth.js +++ b/app/soapbox/actions/auth.js @@ -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,