fix instance login
This commit is contained in:
parent
586ba670b8
commit
a56996b2bf
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue