Skip to content

Commit

Permalink
fix: fix login to limited federation instance (#2147)
Browse files Browse the repository at this point in the history
Fixes #2146
  • Loading branch information
nolanlawson authored May 15, 2022
1 parent 5fd8d0a commit 69bb849
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/routes/_actions/addInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/routes/_actions/instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
8 changes: 5 additions & 3 deletions src/routes/_api/instance.js
Original file line number Diff line number Diff line change
@@ -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 })
}
16 changes: 9 additions & 7 deletions src/routes/_pages/settings/instances/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ <h1 id="add-an-instance-h1">{pageLabel}</h1>

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,
Expand Down

0 comments on commit 69bb849

Please sign in to comment.