1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-09 10:29:57 +00:00
elk/server/api/[server]/login.ts

29 lines
867 B
TypeScript
Raw Normal View History

2022-11-15 14:29:46 +00:00
import { stringifyQuery } from 'ufo'
import { createError, defineEventHandler, getRouterParams, readBody } from 'h3'
import { getApp, getRedirectURI } from '~/server/shared'
2022-11-15 14:29:46 +00:00
export default defineEventHandler(async (event) => {
2022-12-24 00:51:45 +00:00
let { server } = getRouterParams(event)
2023-01-13 18:10:51 +00:00
const { origin, force_login, lang } = await readBody(event)
2022-12-24 00:51:45 +00:00
server = server.toLocaleLowerCase().trim()
2023-01-01 19:30:39 +00:00
const app = await getApp(origin, server)
2022-11-15 14:29:46 +00:00
if (!app) {
throw createError({
statusCode: 400,
statusMessage: `App not registered for server: ${server}`,
})
2022-11-15 14:29:46 +00:00
}
const query = stringifyQuery({
client_id: app.client_id,
force_login: force_login === true ? 'true' : 'false',
2022-11-15 14:29:46 +00:00
scope: 'read write follow push',
2023-01-01 19:30:39 +00:00
redirect_uri: getRedirectURI(origin, server),
2022-11-15 14:29:46 +00:00
response_type: 'code',
2023-01-13 18:10:51 +00:00
lang,
2022-11-15 14:29:46 +00:00
})
return `https://${server}/oauth/authorize?${query}`
2022-11-15 14:29:46 +00:00
})