2022-11-15 15:48:23 +00:00
|
|
|
import { stringifyQuery } from 'vue-router'
|
2022-11-25 16:57:29 +00:00
|
|
|
import { HOST_DOMAIN, getApp } from '~/server/shared'
|
2022-11-15 11:54:07 +00:00
|
|
|
|
2022-11-26 17:56:35 +00:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const server = event.context.params.server
|
2022-11-25 16:57:29 +00:00
|
|
|
const app = await getApp(HOST_DOMAIN, server)
|
2022-11-15 14:29:46 +00:00
|
|
|
|
|
|
|
if (!app) {
|
2022-11-26 17:56:35 +00:00
|
|
|
setResponseStatus(400)
|
2022-11-15 14:29:46 +00:00
|
|
|
return `App not registered for server: ${server}`
|
|
|
|
}
|
|
|
|
|
2022-11-26 17:56:35 +00:00
|
|
|
const { code } = getQuery(event)
|
2022-11-15 11:54:07 +00:00
|
|
|
|
2022-11-15 15:48:23 +00:00
|
|
|
const result: any = await $fetch(`https://${server}/oauth/token`, {
|
2022-11-15 11:54:07 +00:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2022-11-15 14:29:46 +00:00
|
|
|
client_id: app.client_id,
|
|
|
|
client_secret: app.client_secret,
|
2022-11-15 15:48:23 +00:00
|
|
|
redirect_uri: `${HOST_DOMAIN}/api/${server}/oauth`,
|
2022-11-15 11:54:07 +00:00
|
|
|
grant_type: 'authorization_code',
|
|
|
|
code,
|
|
|
|
scope: 'read write follow push',
|
|
|
|
},
|
|
|
|
})
|
2022-11-15 14:29:46 +00:00
|
|
|
|
2022-11-26 17:56:35 +00:00
|
|
|
const url = `${HOST_DOMAIN}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`
|
|
|
|
await sendRedirect(event, url, 302)
|
2022-11-15 11:54:07 +00:00
|
|
|
})
|