fix instance login

This commit is contained in:
Nolan Lawson 2018-01-13 19:23:05 -08:00
parent 586ba670b8
commit a56996b2bf
3 changed files with 18 additions and 11 deletions

View file

@ -12,7 +12,7 @@ export async function post(url, body) {
export function paramsString(paramsObject) { export function paramsString(paramsObject) {
let params = new URLSearchParams() let params = new URLSearchParams()
Object.keys(paramsObject).forEach(key => { Object.keys(paramsObject).forEach(key => {
params.set(key, paramsObject[value]) params.set(key, paramsObject[key])
}) })
return params.toString() return params.toString()
} }

View file

@ -1,35 +1,34 @@
const WEBSITE = 'https://pinafore.social' const WEBSITE = 'https://pinafore.social'
const REDIRECT_URI = (typeof location !== 'undefined' ? location.origin : 'https://pinafore.social') + '/settings/instances'
const SCOPES = 'read write follow' const SCOPES = 'read write follow'
const CLIENT_NAME = 'Pinafore' const CLIENT_NAME = 'Pinafore'
import { post, get, paramsString } from '../ajax' import { post, get, paramsString } from '../ajax'
export function registerApplication(instanceName) { export function registerApplication(instanceName, redirectUri) {
const url = `https://${instanceName}/api/v1/apps` const url = `https://${instanceName}/api/v1/apps`
return post(url, { return post(url, {
client_name: CLIENT_NAME, client_name: CLIENT_NAME,
redirect_uris: REDIRECT_URI, redirect_uris: redirectUri,
scopes: SCOPES, scopes: SCOPES,
website: WEBSITE website: WEBSITE
}) })
} }
export function generateAuthLink(instanceName, clientId) { export function generateAuthLink(instanceName, clientId, redirectUri) {
let params = paramsString({ let params = paramsString({
'client_id': clientId, 'client_id': clientId,
'redirect_uri': REDIRECT_URI, 'redirect_uri': redirectUri,
'response_type': 'code', 'response_type': 'code',
'scope': SCOPES 'scope': SCOPES
}) })
return `https://${instanceName}/oauth/authorize?${params}` return `https://${instanceName}/oauth/authorize?${params}`
} }
export function getAccessTokenFromAuthCode(instanceName, clientId, clientSecret, code) { export function getAccessTokenFromAuthCode(instanceName, clientId, clientSecret, code, redirectUri) {
let url = `https://${instanceName}/oauth/token` let url = `https://${instanceName}/oauth/token`
return post(url, { return post(url, {
client_id: clientId, client_id: clientId,
client_secret: clientSecret, client_secret: clientSecret,
redirect_uri: REDIRECT_URI, redirect_uri: redirectUri,
grant_type: 'authorization_code', grant_type: 'authorization_code',
code: code code: code
}) })

View file

@ -53,6 +53,9 @@
import { store } from '../../_utils/store' import { store } from '../../_utils/store'
import { goto } from 'sapper/runtime.js' import { goto } from 'sapper/runtime.js'
const REDIRECT_URI = (typeof location !== 'undefined' ?
location.origin : 'https://pinafore.social') + '/settings/instances/add'
export default { export default {
oncreate: function () { oncreate: function () {
if (process.browser) { if (process.browser) {
@ -75,14 +78,18 @@
let instanceName = this.store.get('instanceNameInSearch') let instanceName = this.store.get('instanceNameInSearch')
instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '')
// TODO: show toast error if you're already logged into this instance // TODO: show toast error if you're already logged into this instance
let instanceData = await (await registerApplication(instanceName)).json() let instanceData = await registerApplication(instanceName, REDIRECT_URI)
// TODO: handle error // TODO: handle error
this.store.set({ this.store.set({
currentRegisteredInstanceName: instanceName, currentRegisteredInstanceName: instanceName,
currentRegisteredInstance: instanceData currentRegisteredInstance: instanceData
}) })
this.store.save() this.store.save()
let oauthUrl = generateAuthLink(instanceName, instanceData.client_id) let oauthUrl = generateAuthLink(
instanceName,
instanceData.client_id,
REDIRECT_URI
)
document.location.href = oauthUrl document.location.href = oauthUrl
}, },
onReceivedOauthCode: async function(code) { onReceivedOauthCode: async function(code) {
@ -92,7 +99,8 @@
currentRegisteredInstanceName, currentRegisteredInstanceName,
currentRegisteredInstance.client_id, currentRegisteredInstance.client_id,
currentRegisteredInstance.client_secret, currentRegisteredInstance.client_secret,
code code,
REDIRECT_URI
) )
// TODO: handle error // TODO: handle error
let loggedInInstances = this.store.get('loggedInInstances') let loggedInInstances = this.store.get('loggedInInstances')