2018-01-08 01:00:42 +01:00
|
|
|
<:Head>
|
|
|
|
<title>Instance Wizard</title>
|
|
|
|
</:Head>
|
|
|
|
|
|
|
|
<Layout page='settings'>
|
2018-01-09 02:44:29 +01:00
|
|
|
<SettingsLayout page='add-instance'>
|
|
|
|
<h1>Add an instance</h1>
|
2018-01-08 01:00:42 +01:00
|
|
|
|
2018-01-09 02:44:29 +01:00
|
|
|
<p>Log in to your instance to use Pinafore.</p>
|
2018-01-08 01:00:42 +01:00
|
|
|
|
2018-01-09 02:44:29 +01:00
|
|
|
<form on:submit='handleSubmit(event)'>
|
|
|
|
<label for="instanceInput">Instance name:</label>
|
|
|
|
<input type="text" id="instanceInput" bind:value='$instanceNameInSearch' placeholder=''>
|
|
|
|
<button class="primary" type="submit" id="submitButton">Add instance</button>
|
|
|
|
</form>
|
2018-01-08 01:00:42 +01:00
|
|
|
|
2018-01-09 02:44:29 +01:00
|
|
|
<p>Don't have an instance? <a href="https://joinmastodon.org">Join Mastodon!</a></p>
|
|
|
|
</SettingsLayout>
|
2018-01-08 01:00:42 +01:00
|
|
|
</Layout>
|
|
|
|
<style>
|
2018-01-08 07:00:16 +01:00
|
|
|
@media (max-width: 767px) {
|
|
|
|
input {
|
|
|
|
width: 90%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 01:00:42 +01:00
|
|
|
input {
|
2018-01-08 07:00:16 +01:00
|
|
|
width: 250px;
|
2018-01-08 01:00:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
form {
|
|
|
|
background: #fafafa;
|
|
|
|
padding: 5px 10px 15px;
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
margin: 0 auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
form label, form input, form button {
|
|
|
|
display: block;
|
|
|
|
margin: 20px 5px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
import Layout from '../_components/Layout.html';
|
2018-01-09 02:44:29 +01:00
|
|
|
import SettingsLayout from './_components/SettingsLayout.html'
|
2018-01-08 07:00:16 +01:00
|
|
|
import { registerApplication, generateAuthLink, getAccessTokenFromAuthCode } from '../_utils/mastodon'
|
|
|
|
import { databasePromise } from '../_utils/database'
|
2018-01-08 19:13:42 +01:00
|
|
|
import { store } from '../_utils/store'
|
2018-01-08 07:13:15 +01:00
|
|
|
import { goto } from 'sapper/runtime.js'
|
2018-01-08 01:00:42 +01:00
|
|
|
|
|
|
|
export default {
|
2018-01-08 07:00:16 +01:00
|
|
|
oncreate: function () {
|
|
|
|
if (process.browser) {
|
|
|
|
(async () => {
|
|
|
|
let params = new URLSearchParams(location.search)
|
|
|
|
if (params.has('code')) {
|
2018-01-08 19:13:42 +01:00
|
|
|
this.onReceivedOauthCode(params.get('code'))
|
2018-01-08 07:00:16 +01:00
|
|
|
}
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
},
|
2018-01-08 01:00:42 +01:00
|
|
|
components: {
|
2018-01-09 02:44:29 +01:00
|
|
|
Layout,
|
|
|
|
SettingsLayout
|
2018-01-08 01:00:42 +01:00
|
|
|
},
|
2018-01-08 19:13:42 +01:00
|
|
|
store: () => store,
|
2018-01-08 01:00:42 +01:00
|
|
|
methods: {
|
2018-01-08 19:13:42 +01:00
|
|
|
onReceivedOauthCode: async function(code) {
|
|
|
|
let instanceData = this.store.get('currentOauthInstance')
|
|
|
|
instanceData.code = code
|
|
|
|
let response = await (await getAccessTokenFromAuthCode(
|
|
|
|
instanceData.instanceName,
|
|
|
|
instanceData.client_id,
|
|
|
|
instanceData.client_secret,
|
|
|
|
instanceData.code
|
|
|
|
)).json()
|
|
|
|
instanceData = Object.assign(instanceData, response)
|
|
|
|
this.store.set({
|
|
|
|
'currentOauthInstance': instanceData,
|
|
|
|
'instanceNameInSearch': '',
|
|
|
|
})
|
|
|
|
this.store.save()
|
|
|
|
goto('/')
|
|
|
|
},
|
2018-01-09 02:23:33 +01:00
|
|
|
onSubmit: async function(event) {
|
2018-01-08 01:00:42 +01:00
|
|
|
event.preventDefault()
|
2018-01-08 19:13:42 +01:00
|
|
|
let instanceName = this.store.get('instanceNameInSearch')
|
2018-01-08 07:00:16 +01:00
|
|
|
instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '')
|
|
|
|
let data = await (await registerApplication(instanceName)).json()
|
|
|
|
data.instanceName = instanceName
|
2018-01-08 19:13:42 +01:00
|
|
|
this.store.set({'currentOauthInstance': data})
|
|
|
|
this.store.save()
|
2018-01-08 07:00:16 +01:00
|
|
|
let oauthUrl = generateAuthLink(instanceName, data.client_id)
|
|
|
|
document.location.href = oauthUrl
|
|
|
|
},
|
2018-01-08 01:00:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|