elk/server/api/[server]/oauth.ts

36 lines
949 B
TypeScript

import { getQuery } from 'ufo'
import { stringifyQuery } from 'vue-router'
import { HOST_DOMAIN, getApp } from '~/server/shared'
export default defineEventHandler(async ({ context, req, res }) => {
const server = context.params.server
const app = await getApp(HOST_DOMAIN, server)
if (!app) {
res.statusCode = 400
return `App not registered for server: ${server}`
}
const query = getQuery(req.url!)
const code = query.code
const result: any = await $fetch(`https://${server}/oauth/token`, {
method: 'POST',
body: {
client_id: app.client_id,
client_secret: app.client_secret,
redirect_uri: `${HOST_DOMAIN}/api/${server}/oauth`,
grant_type: 'authorization_code',
code,
scope: 'read write follow push',
},
})
res.writeHead(302, {
Location: `${HOST_DOMAIN}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`,
})
res.end()
return result
})