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

30 lines
724 B
TypeScript
Raw Normal View History

2022-11-15 11:54:07 +00:00
import { getQuery } from 'ufo'
2022-11-15 14:29:46 +00:00
import { getApp } from '~/server/shared'
2022-11-15 11:54:07 +00:00
export default defineEventHandler(async (event) => {
2022-11-15 14:29:46 +00:00
const server = event.context.params.server
const app = await getApp(server)
if (!app) {
event.res.statusCode = 400
return `App not registered for server: ${server}`
}
2022-11-15 11:54:07 +00:00
const query = getQuery(event.req.url!)
const code = query.code
const res = await $fetch(`https://${server}/oauth/token`, {
method: 'POST',
body: {
2022-11-15 14:29:46 +00:00
client_id: app.client_id,
client_secret: app.client_secret,
2022-11-15 11:54:07 +00:00
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
grant_type: 'authorization_code',
code,
scope: 'read write follow push',
},
})
2022-11-15 14:29:46 +00:00
2022-11-15 11:54:07 +00:00
console.log({ res })
})