From 2f6b0d4cac5b513a8762bdad30d0be2a2400a78f Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 15 May 2022 09:37:10 -0700 Subject: [PATCH] fix: fix login to limited federation instance Fixes #2146 --- src/routes/_actions/addInstance.js | 12 ++++++++++-- src/routes/_actions/instances.js | 6 +++++- src/routes/_api/instance.js | 8 +++++--- src/routes/_pages/settings/instances/add.html | 16 +++++++++------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/routes/_actions/addInstance.js b/src/routes/_actions/addInstance.js index e3efdc9a..27f47397 100644 --- a/src/routes/_actions/addInstance.js +++ b/src/routes/_actions/addInstance.js @@ -37,8 +37,16 @@ async function redirectToOauth () { } const redirectUri = getRedirectUri() const registrationPromise = registerApplication(instanceNameInSearch, redirectUri) - const instanceInfo = await getInstanceInfo(instanceNameInSearch) - await database.setInstanceInfo(instanceNameInSearch, instanceInfo) // cache for later + try { + const instanceInfo = await getInstanceInfo(instanceNameInSearch) + await database.setInstanceInfo(instanceNameInSearch, instanceInfo) // cache for later + } catch (err) { + // We get a 401 in limited federation mode, so we can just skip setting the instance info in that case. + // It will be fetched automatically later. + if (err.status !== 401) { + throw err // this is a good way to test for typos in the instance name or some other problem + } + } const instanceData = await registrationPromise store.set({ currentRegisteredInstanceName: instanceNameInSearch, diff --git a/src/routes/_actions/instances.js b/src/routes/_actions/instances.js index a9f7df8a..d6526038 100644 --- a/src/routes/_actions/instances.js +++ b/src/routes/_actions/instances.js @@ -111,7 +111,11 @@ export async function updateVerifyCredentialsForCurrentInstance () { export async function updateInstanceInfo (instanceName) { await cacheFirstUpdateAfter( - () => getInstanceInfo(instanceName), + () => { + const { loggedInInstances } = store.get() + const accessToken = loggedInInstances[instanceName] && loggedInInstances[instanceName].access_token + return getInstanceInfo(instanceName, accessToken) + }, () => database.getInstanceInfo(instanceName), info => database.setInstanceInfo(instanceName, info), info => { diff --git a/src/routes/_api/instance.js b/src/routes/_api/instance.js index 7538b8dd..127ce64e 100644 --- a/src/routes/_api/instance.js +++ b/src/routes/_api/instance.js @@ -1,7 +1,9 @@ import { get, DEFAULT_TIMEOUT } from '../_utils/ajax.js' -import { basename } from './utils.js' +import { auth, basename } from './utils.js' -export function getInstanceInfo (instanceName) { +export function getInstanceInfo (instanceName, accessToken) { const url = `${basename(instanceName)}/api/v1/instance` - return get(url, null, { timeout: DEFAULT_TIMEOUT }) + // accessToken is required in limited federation mode, but elsewhere we don't need it (e.g. during login) + const headers = accessToken ? auth(accessToken) : null + return get(url, headers, { timeout: DEFAULT_TIMEOUT }) } diff --git a/src/routes/_pages/settings/instances/add.html b/src/routes/_pages/settings/instances/add.html index b57af49a..788c4a28 100644 --- a/src/routes/_pages/settings/instances/add.html +++ b/src/routes/_pages/settings/instances/add.html @@ -89,14 +89,16 @@ export default { async oncreate () { - const codeMatch = location.search.match(/code=([^&]+)/) - if (codeMatch) { - return handleOauthCode(codeMatch[1]) + const params = new URLSearchParams(location.search) + const code = params.get('code') + if (code) { + await handleOauthCode(code) + } else { + this.set({ + hasIndexedDB: await testHasIndexedDB(), + hasLocalStorage: testHasLocalStorage() + }) } - this.set({ - hasIndexedDB: await testHasIndexedDB(), - hasLocalStorage: testHasLocalStorage() - }) }, components: { SettingsLayout,