2022-08-11 19:50:08 -07:00
|
|
|
import axios from 'axios';
|
|
|
|
|
2022-11-15 12:48:54 -08:00
|
|
|
import * as BuildConfig from 'soapbox/build-config';
|
2022-08-11 19:50:08 -07:00
|
|
|
import { isURL } from 'soapbox/utils/auth';
|
|
|
|
import sourceCode from 'soapbox/utils/code';
|
2023-01-13 16:42:41 -08:00
|
|
|
import { getScopes } from 'soapbox/utils/scopes';
|
2022-08-11 19:50:08 -07:00
|
|
|
|
|
|
|
import { createApp } from './apps';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
|
|
|
const createProviderApp = () => {
|
|
|
|
return async(dispatch: AppDispatch, getState: () => RootState) => {
|
2023-01-13 16:42:41 -08:00
|
|
|
const scopes = getScopes(getState());
|
2022-08-11 19:50:08 -07:00
|
|
|
|
|
|
|
const params = {
|
|
|
|
client_name: sourceCode.displayName,
|
|
|
|
redirect_uris: `${window.location.origin}/login/external`,
|
|
|
|
website: sourceCode.homepage,
|
|
|
|
scopes,
|
|
|
|
};
|
|
|
|
|
|
|
|
return dispatch(createApp(params));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const prepareRequest = (provider: string) => {
|
|
|
|
return async(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const baseURL = isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : '';
|
|
|
|
|
2023-01-13 16:42:41 -08:00
|
|
|
const scopes = getScopes(getState());
|
2022-08-11 19:50:08 -07:00
|
|
|
const app = await dispatch(createProviderApp());
|
|
|
|
const { client_id, redirect_uri } = app;
|
|
|
|
|
|
|
|
localStorage.setItem('soapbox:external:app', JSON.stringify(app));
|
|
|
|
localStorage.setItem('soapbox:external:baseurl', baseURL);
|
|
|
|
localStorage.setItem('soapbox:external:scopes', scopes);
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
provider,
|
|
|
|
authorization: {
|
|
|
|
client_id,
|
|
|
|
redirect_uri,
|
|
|
|
scope: scopes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const formdata = axios.toFormData(params);
|
|
|
|
const query = new URLSearchParams(formdata as any);
|
|
|
|
|
|
|
|
location.href = `${baseURL}/oauth/prepare_request?${query.toString()}`;
|
|
|
|
};
|
|
|
|
};
|