diff --git a/routes/_utils/ajax.js b/routes/_utils/ajax.js index dd6ca14..6594355 100644 --- a/routes/_utils/ajax.js +++ b/routes/_utils/ajax.js @@ -12,7 +12,7 @@ export async function post(url, body) { export function paramsString(paramsObject) { let params = new URLSearchParams() Object.keys(paramsObject).forEach(key => { - params.set(key, paramsObject[value]) + params.set(key, paramsObject[key]) }) return params.toString() } diff --git a/routes/_utils/mastodon/oauth.js b/routes/_utils/mastodon/oauth.js index 9ff75b1..bb7b4e6 100644 --- a/routes/_utils/mastodon/oauth.js +++ b/routes/_utils/mastodon/oauth.js @@ -1,35 +1,34 @@ const WEBSITE = 'https://pinafore.social' -const REDIRECT_URI = (typeof location !== 'undefined' ? location.origin : 'https://pinafore.social') + '/settings/instances' const SCOPES = 'read write follow' const CLIENT_NAME = 'Pinafore' import { post, get, paramsString } from '../ajax' -export function registerApplication(instanceName) { +export function registerApplication(instanceName, redirectUri) { const url = `https://${instanceName}/api/v1/apps` return post(url, { client_name: CLIENT_NAME, - redirect_uris: REDIRECT_URI, + redirect_uris: redirectUri, scopes: SCOPES, website: WEBSITE }) } -export function generateAuthLink(instanceName, clientId) { +export function generateAuthLink(instanceName, clientId, redirectUri) { let params = paramsString({ 'client_id': clientId, - 'redirect_uri': REDIRECT_URI, + 'redirect_uri': redirectUri, 'response_type': 'code', 'scope': SCOPES }) 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` return post(url, { client_id: clientId, client_secret: clientSecret, - redirect_uri: REDIRECT_URI, + redirect_uri: redirectUri, grant_type: 'authorization_code', code: code }) diff --git a/routes/settings/instances/add.html b/routes/settings/instances/add.html index b51a569..92ca4fc 100644 --- a/routes/settings/instances/add.html +++ b/routes/settings/instances/add.html @@ -53,6 +53,9 @@ import { store } from '../../_utils/store' import { goto } from 'sapper/runtime.js' + const REDIRECT_URI = (typeof location !== 'undefined' ? + location.origin : 'https://pinafore.social') + '/settings/instances/add' + export default { oncreate: function () { if (process.browser) { @@ -75,14 +78,18 @@ let instanceName = this.store.get('instanceNameInSearch') instanceName = instanceName.replace(/^https?:\/\//, '').replace('/$', '') // 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 this.store.set({ currentRegisteredInstanceName: instanceName, currentRegisteredInstance: instanceData }) this.store.save() - let oauthUrl = generateAuthLink(instanceName, instanceData.client_id) + let oauthUrl = generateAuthLink( + instanceName, + instanceData.client_id, + REDIRECT_URI + ) document.location.href = oauthUrl }, onReceivedOauthCode: async function(code) { @@ -92,7 +99,8 @@ currentRegisteredInstanceName, currentRegisteredInstance.client_id, currentRegisteredInstance.client_secret, - code + code, + REDIRECT_URI ) // TODO: handle error let loggedInInstances = this.store.get('loggedInInstances')