2021-08-22 08:52:02 -07:00
|
|
|
/**
|
|
|
|
* OAuth: create and revoke tokens.
|
|
|
|
* Tokens can be used by users and apps.
|
|
|
|
* https://docs.joinmastodon.org/methods/apps/oauth/
|
2024-08-28 04:41:08 -07:00
|
|
|
* @module pl-fe/actions/oauth
|
|
|
|
* @see module:pl-fe/actions/auth
|
2021-08-22 08:52:02 -07:00
|
|
|
*/
|
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
import { PlApiClient, type GetTokenParams, type RevokeTokenParams } from 'pl-api';
|
2023-05-18 08:17:55 -07:00
|
|
|
|
2024-08-28 04:41:08 -07:00
|
|
|
import * as BuildConfig from 'pl-fe/build-config';
|
|
|
|
import { getBaseURL } from 'pl-fe/utils/state';
|
2021-08-21 17:37:28 -07:00
|
|
|
|
2024-08-28 04:41:08 -07:00
|
|
|
import type { AppDispatch, RootState } from 'pl-fe/store';
|
2022-06-10 10:56:22 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const OAUTH_TOKEN_CREATE_REQUEST = 'OAUTH_TOKEN_CREATE_REQUEST' as const;
|
|
|
|
const OAUTH_TOKEN_CREATE_SUCCESS = 'OAUTH_TOKEN_CREATE_SUCCESS' as const;
|
|
|
|
const OAUTH_TOKEN_CREATE_FAIL = 'OAUTH_TOKEN_CREATE_FAIL' as const;
|
2021-08-21 17:37:28 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const OAUTH_TOKEN_REVOKE_REQUEST = 'OAUTH_TOKEN_REVOKE_REQUEST' as const;
|
|
|
|
const OAUTH_TOKEN_REVOKE_SUCCESS = 'OAUTH_TOKEN_REVOKE_SUCCESS' as const;
|
|
|
|
const OAUTH_TOKEN_REVOKE_FAIL = 'OAUTH_TOKEN_REVOKE_FAIL' as const;
|
2021-08-21 17:37:28 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
const obtainOAuthToken = (params: GetTokenParams, baseURL?: string) =>
|
2022-06-10 10:56:22 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2021-08-21 17:37:28 -07:00
|
|
|
dispatch({ type: OAUTH_TOKEN_CREATE_REQUEST, params });
|
2024-08-19 12:51:43 -07:00
|
|
|
const client = new PlApiClient(baseURL || BuildConfig.BACKEND_URL || '');
|
2024-08-04 07:09:52 -07:00
|
|
|
|
|
|
|
return client.oauth.getToken(params).then((token) => {
|
2021-08-21 17:37:28 -07:00
|
|
|
dispatch({ type: OAUTH_TOKEN_CREATE_SUCCESS, params, token });
|
|
|
|
return token;
|
|
|
|
}).catch(error => {
|
2021-09-08 09:08:22 -07:00
|
|
|
dispatch({ type: OAUTH_TOKEN_CREATE_FAIL, params, error, skipAlert: true });
|
2021-08-21 17:37:28 -07:00
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
const revokeOAuthToken = (params: RevokeTokenParams) =>
|
2023-05-18 08:17:55 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2021-08-21 17:37:28 -07:00
|
|
|
dispatch({ type: OAUTH_TOKEN_REVOKE_REQUEST, params });
|
2023-05-18 08:17:55 -07:00
|
|
|
const baseURL = getBaseURL(getState());
|
2024-08-19 12:51:43 -07:00
|
|
|
const client = new PlApiClient(baseURL || '');
|
2024-08-04 07:09:52 -07:00
|
|
|
return client.oauth.revokeToken(params).then((data) => {
|
2021-08-21 17:37:28 -07:00
|
|
|
dispatch({ type: OAUTH_TOKEN_REVOKE_SUCCESS, params, data });
|
|
|
|
return data;
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch({ type: OAUTH_TOKEN_REVOKE_FAIL, params, error });
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
};
|
2024-05-12 16:18:04 -07:00
|
|
|
|
|
|
|
export {
|
|
|
|
OAUTH_TOKEN_CREATE_REQUEST,
|
|
|
|
OAUTH_TOKEN_CREATE_SUCCESS,
|
|
|
|
OAUTH_TOKEN_CREATE_FAIL,
|
|
|
|
OAUTH_TOKEN_REVOKE_REQUEST,
|
|
|
|
OAUTH_TOKEN_REVOKE_SUCCESS,
|
|
|
|
OAUTH_TOKEN_REVOKE_FAIL,
|
|
|
|
obtainOAuthToken,
|
|
|
|
revokeOAuthToken,
|
|
|
|
};
|