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

37 lines
964 B
TypeScript
Raw Normal View History

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