2018-02-09 01:51:48 +00:00
|
|
|
import { getAccessTokenFromAuthCode, registerApplication, generateAuthLink } from '../_api/oauth'
|
|
|
|
import { getInstanceInfo } from '../_api/instance'
|
2018-01-27 21:38:57 +00:00
|
|
|
import { goto } from 'sapper/runtime.js'
|
2018-02-09 01:51:48 +00:00
|
|
|
import { switchToTheme } from '../_utils/themeEngine'
|
|
|
|
import { store } from '../_store/store'
|
|
|
|
import { updateVerifyCredentialsForInstance } from './instances'
|
2018-02-28 07:18:07 +00:00
|
|
|
import { updateCustomEmojiForInstance } from './emoji'
|
2018-08-30 02:03:12 +00:00
|
|
|
import { database } from '../_database/database'
|
2018-01-27 21:38:57 +00:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
const REDIRECT_URI = (typeof location !== 'undefined'
|
|
|
|
? location.origin : 'https://pinafore.social') + '/settings/instances/add'
|
2018-01-27 21:38:57 +00:00
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function redirectToOauth () {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { instanceNameInSearch, loggedInInstances } = store.get()
|
|
|
|
instanceNameInSearch = instanceNameInSearch.replace(/^https?:\/\//, '').replace(/\/$/, '').replace('/$', '').toLowerCase()
|
|
|
|
if (Object.keys(loggedInInstances).includes(instanceNameInSearch)) {
|
2018-12-06 05:34:02 +00:00
|
|
|
let err = new Error(`You've already logged in to ${instanceNameInSearch}`)
|
|
|
|
err.knownError = true
|
|
|
|
throw err
|
2018-01-27 21:38:57 +00:00
|
|
|
}
|
2018-04-19 16:37:05 +00:00
|
|
|
let registrationPromise = registerApplication(instanceNameInSearch, REDIRECT_URI)
|
|
|
|
let instanceInfo = await getInstanceInfo(instanceNameInSearch)
|
2018-08-30 02:03:12 +00:00
|
|
|
await database.setInstanceInfo(instanceNameInSearch, instanceInfo) // cache for later
|
2018-01-27 21:38:57 +00:00
|
|
|
let instanceData = await registrationPromise
|
|
|
|
store.set({
|
2018-04-19 16:37:05 +00:00
|
|
|
currentRegisteredInstanceName: instanceNameInSearch,
|
2018-01-27 21:38:57 +00:00
|
|
|
currentRegisteredInstance: instanceData
|
|
|
|
})
|
|
|
|
store.save()
|
|
|
|
let oauthUrl = generateAuthLink(
|
2018-04-19 16:37:05 +00:00
|
|
|
instanceNameInSearch,
|
2018-01-27 21:38:57 +00:00
|
|
|
instanceData.client_id,
|
|
|
|
REDIRECT_URI
|
|
|
|
)
|
|
|
|
document.location.href = oauthUrl
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function logInToInstance () {
|
2018-02-15 02:15:14 +00:00
|
|
|
store.set({
|
|
|
|
logInToInstanceLoading: true,
|
|
|
|
logInToInstanceError: null
|
|
|
|
})
|
2018-01-27 21:38:57 +00:00
|
|
|
try {
|
|
|
|
await redirectToOauth()
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
let error = `${err.message || err.name}. ` +
|
2018-12-06 05:34:02 +00:00
|
|
|
(err.knownError ? '' : (navigator.onLine
|
2018-08-28 13:44:58 +00:00
|
|
|
? `Is this a valid Mastodon instance? Is a browser extension
|
|
|
|
blocking the request? Are you in private browsing mode?`
|
2018-12-06 05:34:02 +00:00
|
|
|
: `Are you offline?`))
|
2018-04-19 16:37:05 +00:00
|
|
|
let { instanceNameInSearch } = store.get()
|
2018-02-18 22:31:28 +00:00
|
|
|
store.set({
|
|
|
|
logInToInstanceError: error,
|
2018-04-19 16:37:05 +00:00
|
|
|
logInToInstanceErrorForText: instanceNameInSearch
|
2018-02-18 22:31:28 +00:00
|
|
|
})
|
2018-01-27 21:38:57 +00:00
|
|
|
} finally {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ logInToInstanceLoading: false })
|
2018-01-27 21:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
async function registerNewInstance (code) {
|
2018-04-19 16:37:05 +00:00
|
|
|
let { currentRegisteredInstanceName, currentRegisteredInstance } = store.get()
|
2018-01-27 21:38:57 +00:00
|
|
|
let instanceData = await getAccessTokenFromAuthCode(
|
|
|
|
currentRegisteredInstanceName,
|
|
|
|
currentRegisteredInstance.client_id,
|
|
|
|
currentRegisteredInstance.client_secret,
|
|
|
|
code,
|
|
|
|
REDIRECT_URI
|
|
|
|
)
|
2018-04-19 16:37:05 +00:00
|
|
|
let { loggedInInstances, loggedInInstancesInOrder, instanceThemes } = store.get()
|
2018-01-27 21:38:57 +00: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 07:18:07 +00:00
|
|
|
// fire off these requests so they're cached
|
|
|
|
/* no await */ updateVerifyCredentialsForInstance(currentRegisteredInstanceName)
|
|
|
|
/* no await */ updateCustomEmojiForInstance(currentRegisteredInstanceName)
|
2018-01-27 21:38:57 +00:00
|
|
|
goto('/')
|
|
|
|
}
|
|
|
|
|
2018-02-09 06:29:29 +00:00
|
|
|
export async function handleOauthCode (code) {
|
2018-01-27 21:38:57 +00:00
|
|
|
try {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ logInToInstanceLoading: true })
|
2018-01-27 21:38:57 +00:00
|
|
|
await registerNewInstance(code)
|
|
|
|
} catch (err) {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ logInToInstanceError: `${err.message || err.name}. Failed to connect to instance.` })
|
2018-01-27 21:38:57 +00:00
|
|
|
} finally {
|
2018-08-30 04:42:57 +00:00
|
|
|
store.set({ logInToInstanceLoading: false })
|
2018-01-27 21:38:57 +00:00
|
|
|
}
|
|
|
|
}
|