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

View file

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