fix: fix login to limited federation instance (#2147)

Fixes #2146
This commit is contained in:
Nolan Lawson 2022-05-15 10:10:04 -07:00 committed by GitHub
parent 5fd8d0ac23
commit 69bb849508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 13 deletions

View File

@ -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,

View File

@ -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 => {

View File

@ -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 })
}

View File

@ -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,