1
0
elk/server/api/[server]/oauth.ts
2022-11-15 23:48:23 +08:00

37 lines
964 B
TypeScript

import { getQuery } from 'ufo'
import { stringifyQuery } from 'vue-router'
import { getApp } from '~/server/shared'
import { HOST_DOMAIN } from '~/constants'
export default defineEventHandler(async ({ context, req, res }) => {
const server = context.params.server
const app = await getApp(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}/login/callback?${stringifyQuery({ server, token: result.access_token })}`,
})
res.end()
return result
})