feat: basic oauth
This commit is contained in:
parent
72b13f5265
commit
7ab17001f0
16 changed files with 199 additions and 106 deletions
|
@ -1,29 +1,36 @@
|
|||
import { getQuery } from 'ufo'
|
||||
import { stringifyQuery } from 'vue-router'
|
||||
import { getApp } from '~/server/shared'
|
||||
import { HOST_DOMAIN } from '~/constants'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const server = event.context.params.server
|
||||
export default defineEventHandler(async ({ context, req, res }) => {
|
||||
const server = context.params.server
|
||||
const app = await getApp(server)
|
||||
|
||||
if (!app) {
|
||||
event.res.statusCode = 400
|
||||
res.statusCode = 400
|
||||
return `App not registered for server: ${server}`
|
||||
}
|
||||
|
||||
const query = getQuery(event.req.url!)
|
||||
const query = getQuery(req.url!)
|
||||
const code = query.code
|
||||
|
||||
const res = await $fetch(`https://${server}/oauth/token`, {
|
||||
const result: any = await $fetch(`https://${server}/oauth/token`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
client_id: app.client_id,
|
||||
client_secret: app.client_secret,
|
||||
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
|
||||
redirect_uri: `${HOST_DOMAIN}/api/${server}/oauth`,
|
||||
grant_type: 'authorization_code',
|
||||
code,
|
||||
scope: 'read write follow push',
|
||||
},
|
||||
})
|
||||
|
||||
console.log({ res })
|
||||
res.writeHead(302, {
|
||||
Location: `${HOST_DOMAIN}/login/callback?${stringifyQuery({ server, token: result.access_token })}`,
|
||||
})
|
||||
res.end()
|
||||
|
||||
return result
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue