elk/server/api/[server]/oauth.ts
Daniel Roe 76715d7cec
fix: missed from commit 🤦‍♂️
2022-11-27 18:44:19 +00:00

29 lines
851 B
TypeScript

import { stringifyQuery } from 'vue-router'
import { HOST_URL, getApp } from '~/server/shared'
export default defineEventHandler(async (event) => {
const server = event.context.params.server
const app = await getApp(server)
if (!app) {
event.node.res.statusCode = 400
return `App not registered for server: ${server}`
}
const { code } = getQuery(event)
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_URL}/api/${server}/oauth`,
grant_type: 'authorization_code',
code,
scope: 'read write follow push',
},
})
const url = `${HOST_URL}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`
await sendRedirect(event, url, 302)
})