mirror of
https://github.com/elk-zone/elk.git
synced 2024-11-28 21:38:52 +03:00
0fba07e6e5
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com> Co-authored-by: Daniel Roe <daniel@roe.dev>
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { stringifyQuery } from 'ufo'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
let { server, origin } = getRouterParams(event)
|
|
server = server.toLocaleLowerCase().trim()
|
|
origin = decodeURIComponent(origin)
|
|
const app = await getApp(origin, server)
|
|
|
|
if (!app) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: `App not registered for server: ${server}`,
|
|
})
|
|
}
|
|
|
|
const { code } = getQuery(event)
|
|
if (!code) {
|
|
throw createError({
|
|
statusCode: 422,
|
|
statusMessage: 'Missing authentication code.',
|
|
})
|
|
}
|
|
|
|
try {
|
|
const result: any = await $fetch(`https://${server}/oauth/token`, {
|
|
method: 'POST',
|
|
body: {
|
|
client_id: app.client_id,
|
|
client_secret: app.client_secret,
|
|
redirect_uri: getRedirectURI(origin, server),
|
|
grant_type: 'authorization_code',
|
|
code,
|
|
scope: 'read write follow push',
|
|
},
|
|
retry: 3,
|
|
})
|
|
|
|
const url = `/signin/callback?${stringifyQuery({ server, token: result.access_token, vapid_key: app.vapid_key })}`
|
|
await sendRedirect(event, url, 302)
|
|
}
|
|
catch {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Could not complete log in.',
|
|
})
|
|
}
|
|
})
|