pinafore/routes/_actions/addInstance.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

import { getAccessTokenFromAuthCode, registerApplication, generateAuthLink } from '../_api/oauth'
import { getInstanceInfo } from '../_api/instance'
2018-01-27 22:38:57 +01:00
import { goto } from 'sapper/runtime.js'
import { switchToTheme } from '../_utils/themeEngine'
import { store } from '../_store/store'
import { updateVerifyCredentialsForInstance } from './instances'
2018-02-28 08:18:07 +01:00
import { updateCustomEmojiForInstance } from './emoji'
import { setInstanceInfo as setInstanceInfoInDatabase } from '../_database/meta'
2018-01-27 22:38:57 +01:00
2018-02-09 07:29:29 +01:00
const REDIRECT_URI = (typeof location !== 'undefined'
? location.origin : 'https://pinafore.social') + '/settings/instances/add'
2018-01-27 22:38:57 +01:00
2018-02-09 07:29:29 +01:00
async function redirectToOauth () {
let { instanceNameInSearch, loggedInInstances } = store.get()
instanceNameInSearch = instanceNameInSearch.replace(/^https?:\/\//, '').replace(/\/$/, '').replace('/$', '').toLowerCase()
if (Object.keys(loggedInInstances).includes(instanceNameInSearch)) {
store.set({logInToInstanceError: `You've already logged in to ${instanceNameInSearch}`})
2018-01-27 22:38:57 +01:00
return
}
let registrationPromise = registerApplication(instanceNameInSearch, REDIRECT_URI)
let instanceInfo = await getInstanceInfo(instanceNameInSearch)
await setInstanceInfoInDatabase(instanceNameInSearch, instanceInfo) // cache for later
2018-01-27 22:38:57 +01:00
let instanceData = await registrationPromise
store.set({
currentRegisteredInstanceName: instanceNameInSearch,
2018-01-27 22:38:57 +01:00
currentRegisteredInstance: instanceData
})
store.save()
let oauthUrl = generateAuthLink(
instanceNameInSearch,
2018-01-27 22:38:57 +01:00
instanceData.client_id,
REDIRECT_URI
)
document.location.href = oauthUrl
}
2018-02-09 07:29:29 +01:00
export async function logInToInstance () {
2018-02-15 03:15:14 +01:00
store.set({
logInToInstanceLoading: true,
logInToInstanceError: null
})
2018-01-27 22:38:57 +01:00
try {
await redirectToOauth()
} catch (err) {
console.error(err)
let error = `${err.message || err.name}. ` +
2018-02-09 07:29:29 +01:00
(navigator.onLine
? `Is this a valid Mastodon instance? Is a browser extension blocking the request?`
2018-02-09 07:29:29 +01:00
: `Are you offline?`)
let { instanceNameInSearch } = store.get()
2018-02-18 23:31:28 +01:00
store.set({
logInToInstanceError: error,
logInToInstanceErrorForText: instanceNameInSearch
2018-02-18 23:31:28 +01:00
})
2018-01-27 22:38:57 +01:00
} finally {
store.set({logInToInstanceLoading: false})
}
}
2018-02-09 07:29:29 +01:00
async function registerNewInstance (code) {
let { currentRegisteredInstanceName, currentRegisteredInstance } = store.get()
2018-01-27 22:38:57 +01:00
let instanceData = await getAccessTokenFromAuthCode(
currentRegisteredInstanceName,
currentRegisteredInstance.client_id,
currentRegisteredInstance.client_secret,
code,
REDIRECT_URI
)
let { loggedInInstances, loggedInInstancesInOrder, instanceThemes } = store.get()
2018-01-27 22:38:57 +01:00
instanceThemes[currentRegisteredInstanceName] = 'default'
loggedInInstances[currentRegisteredInstanceName] = instanceData
if (!loggedInInstancesInOrder.includes(currentRegisteredInstanceName)) {
loggedInInstancesInOrder.push(currentRegisteredInstanceName)
}
store.set({
instanceNameInSearch: '',
currentRegisteredInstanceName: null,
currentRegisteredInstance: null,
loggedInInstances: loggedInInstances,
currentInstance: currentRegisteredInstanceName,
loggedInInstancesInOrder: loggedInInstancesInOrder,
instanceThemes: instanceThemes
})
store.save()
switchToTheme('default')
2018-02-28 08:18:07 +01:00
// fire off these requests so they're cached
/* no await */ updateVerifyCredentialsForInstance(currentRegisteredInstanceName)
/* no await */ updateCustomEmojiForInstance(currentRegisteredInstanceName)
2018-01-27 22:38:57 +01:00
goto('/')
}
2018-02-09 07:29:29 +01:00
export async function handleOauthCode (code) {
2018-01-27 22:38:57 +01:00
try {
store.set({logInToInstanceLoading: true})
await registerNewInstance(code)
} catch (err) {
store.set({logInToInstanceError: `${err.message || err.name}. Failed to connect to instance.`})
} finally {
store.set({logInToInstanceLoading: false})
}
}