pinafore/routes/settings/add-instance.html

94 lines
2.6 KiB
HTML
Raw Normal View History

2018-01-08 00:00:42 +00:00
<:Head>
<title>Instance Wizard</title>
</:Head>
<Layout page='settings'>
<h1>Add an instance</h1>
<p>Log in to your instance to use Pinafore.</p>
<form on:submit='handleSubmit(event)'>
<label for="instanceInput">Instance name:</label>
<input type="text" id="instanceInput" bind:value='instanceName' placeholder=''>
<button class="primary" type="submit" id="submitButton">Add instance</button>
</form>
<p>Don't have an instance? <a href="https://joinmastodon.org">Join Mastodon!</a></p>
</Layout>
<style>
2018-01-08 06:00:16 +00:00
@media (max-width: 767px) {
input {
width: 90%;
}
}
2018-01-08 00:00:42 +00:00
input {
2018-01-08 06:00:16 +00:00
width: 250px;
2018-01-08 00:00:42 +00: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;
}
2018-01-08 06:00:16 +00:00
2018-01-08 00:00:42 +00:00
</style>
<script>
import Layout from '../_components/Layout.html';
2018-01-08 06:00:16 +00:00
import { registerApplication, generateAuthLink, getAccessTokenFromAuthCode } from '../_utils/mastodon'
import { databasePromise } from '../_utils/database'
2018-01-08 06:13:15 +00:00
import { goto } from 'sapper/runtime.js'
2018-01-08 00:00:42 +00:00
export default {
2018-01-08 06:00:16 +00:00
oncreate: function () {
if (process.browser) {
(async () => {
let params = new URLSearchParams(location.search)
if (params.has('code')) {
let db = await databasePromise
let instanceData = await db.get('instance')
this.set({instanceName: instanceData.instanceName})
let code = params.get('code')
instanceData.code = code
let response = await (await getAccessTokenFromAuthCode(
instanceData.instanceName,
instanceData.client_id,
instanceData.client_secret,
instanceData.code
)).json()
instanceData = Object.assign(instanceData, response)
await db.set(`instance`, instanceData)
2018-01-08 06:13:15 +00:00
goto('/')
2018-01-08 06:00:16 +00:00
}
})()
}
},
2018-01-08 00:00:42 +00:00
components: {
Layout
},
data: () => ({
instanceName: ''
}),
methods: {
2018-01-08 06:00:16 +00:00
handleSubmit: async function(event) {
2018-01-08 00:00:42 +00:00
event.preventDefault()
let instanceName = this.get('instanceName')
2018-01-08 06:00:16 +00:00
instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '')
let data = await (await registerApplication(instanceName)).json()
let db = await databasePromise
data.instanceName = instanceName
await db.set(`instance`, data)
let oauthUrl = generateAuthLink(instanceName, data.client_id)
document.location.href = oauthUrl
},
2018-01-08 00:00:42 +00:00
}
}
</script>