2022-11-15 18:48:23 +03:00
|
|
|
import { stringifyQuery } from 'vue-router'
|
2022-12-22 05:12:37 +03:00
|
|
|
import { createError, defineEventHandler, getQuery, getRouterParams, sendRedirect } from 'h3'
|
2022-11-30 01:55:53 +03:00
|
|
|
import { getApp, getRedirectURI } from '~/server/shared'
|
2022-11-15 14:54:07 +03:00
|
|
|
|
2022-11-26 20:56:35 +03:00
|
|
|
export default defineEventHandler(async (event) => {
|
2022-12-24 03:51:45 +03:00
|
|
|
let { server } = getRouterParams(event)
|
|
|
|
server = server.toLocaleLowerCase().trim()
|
2022-11-27 21:44:19 +03:00
|
|
|
const app = await getApp(server)
|
2022-11-15 17:29:46 +03:00
|
|
|
|
|
|
|
if (!app) {
|
2022-11-30 01:55:53 +03:00
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
statusMessage: `App not registered for server: ${server}`,
|
|
|
|
})
|
2022-11-15 17:29:46 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 20:56:35 +03:00
|
|
|
const { code } = getQuery(event)
|
2022-11-30 01:55:53 +03:00
|
|
|
if (!code) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 422,
|
|
|
|
statusMessage: 'Missing authentication code.',
|
|
|
|
})
|
|
|
|
}
|
2022-11-15 14:54:07 +03:00
|
|
|
|
2022-11-15 18:48:23 +03:00
|
|
|
const result: any = await $fetch(`https://${server}/oauth/token`, {
|
2022-11-15 14:54:07 +03:00
|
|
|
method: 'POST',
|
|
|
|
body: {
|
2022-11-15 17:29:46 +03:00
|
|
|
client_id: app.client_id,
|
|
|
|
client_secret: app.client_secret,
|
2022-11-30 01:55:53 +03:00
|
|
|
redirect_uri: getRedirectURI(server),
|
2022-11-15 14:54:07 +03:00
|
|
|
grant_type: 'authorization_code',
|
|
|
|
code,
|
|
|
|
scope: 'read write follow push',
|
|
|
|
},
|
|
|
|
})
|
2022-11-15 17:29:46 +03:00
|
|
|
|
2022-12-18 02:29:16 +03:00
|
|
|
const url = `/signin/callback?${stringifyQuery({ server, token: result.access_token, vapid_key: app.vapid_key })}`
|
2022-11-26 20:56:35 +03:00
|
|
|
await sendRedirect(event, url, 302)
|
2022-11-15 14:54:07 +03:00
|
|
|
})
|