refactor: use h3 utils + event in server apis

This commit is contained in:
Daniel Roe 2022-11-26 17:56:35 +00:00
parent 40a510f43d
commit 0f0acd4824
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55
2 changed files with 10 additions and 19 deletions

View file

@ -1,12 +1,12 @@
import { stringifyQuery } from 'ufo' import { stringifyQuery } from 'ufo'
import { HOST_DOMAIN, getApp } from '~/server/shared' import { HOST_DOMAIN, getApp } from '~/server/shared'
export default defineEventHandler(async ({ context, res }) => { export default defineEventHandler(async (event) => {
const server = context.params.server const server = event.context.params.server
const app = await getApp(HOST_DOMAIN, server) const app = await getApp(HOST_DOMAIN, server)
if (!app) { if (!app) {
res.statusCode = 400 setResponseStatus(400)
return `App not registered for server: ${server}` return `App not registered for server: ${server}`
} }
@ -18,8 +18,5 @@ export default defineEventHandler(async ({ context, res }) => {
}) })
const url = `https://${server}/oauth/authorize?${query}` const url = `https://${server}/oauth/authorize?${query}`
res.writeHead(302, { await sendRedirect(event, url, 302)
Location: url,
})
res.end()
}) })

View file

@ -1,18 +1,16 @@
import { getQuery } from 'ufo'
import { stringifyQuery } from 'vue-router' import { stringifyQuery } from 'vue-router'
import { HOST_DOMAIN, getApp } from '~/server/shared' import { HOST_DOMAIN, getApp } from '~/server/shared'
export default defineEventHandler(async ({ context, req, res }) => { export default defineEventHandler(async (event) => {
const server = context.params.server const server = event.context.params.server
const app = await getApp(HOST_DOMAIN, server) const app = await getApp(HOST_DOMAIN, server)
if (!app) { if (!app) {
res.statusCode = 400 setResponseStatus(400)
return `App not registered for server: ${server}` return `App not registered for server: ${server}`
} }
const query = getQuery(req.url!) const { code } = getQuery(event)
const code = query.code
const result: any = await $fetch(`https://${server}/oauth/token`, { const result: any = await $fetch(`https://${server}/oauth/token`, {
method: 'POST', method: 'POST',
@ -26,10 +24,6 @@ export default defineEventHandler(async ({ context, req, res }) => {
}, },
}) })
res.writeHead(302, { const url = `${HOST_DOMAIN}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`
Location: `${HOST_DOMAIN}/signin/callback?${stringifyQuery({ server, token: result.access_token })}`, await sendRedirect(event, url, 302)
})
res.end()
return result
}) })