Apply auth for remote-instance API calls

This commit is contained in:
Lim Chee Aun 2023-12-23 23:07:08 +08:00
parent 6703b27bfb
commit 8ca768b957
2 changed files with 22 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import store from './store';
import {
getAccount,
getAccountByAccessToken,
getAccountByInstance,
getCurrentAccount,
saveAccount,
} from './store-utils';
@ -249,6 +250,22 @@ export function api({ instance, accessToken, accountID, account } = {}) {
};
}
const instanceAccount = getAccountByInstance(instance);
if (instanceAccount) {
const accessToken = instanceAccount.accessToken;
const client =
accountApis[instance]?.[accessToken] ||
initClient({ instance, accessToken });
const { masto, streaming } = client;
return {
masto,
streaming,
client,
authenticated: true,
instance,
};
}
const client = apis[instance] || initClient({ instance });
const { masto, streaming, accessToken } = client;
return {

View file

@ -10,6 +10,11 @@ export function getAccountByAccessToken(accessToken) {
return accounts.find((a) => a.accessToken === accessToken);
}
export function getAccountByInstance(instance) {
const accounts = store.local.getJSON('accounts') || [];
return accounts.find((a) => a.instanceURL === instance);
}
export function getCurrentAccount() {
if (!window.__IGNORE_GET_ACCOUNT_ERROR__) {
// Track down getCurrentAccount() calls before account-based states are initialized