From c684a46a11cbf5e9c98dc6c244c9e1c3d7d79968 Mon Sep 17 00:00:00 2001 From: NextFire Date: Wed, 10 Apr 2024 10:36:34 -0400 Subject: [PATCH] move /api/discord, unhardcode endpoints --- server/routes/_oauth.get.ts | 2 +- server/{api => routes}/discord/auth.get.ts | 0 .../{api => routes}/discord/callback.get.ts | 0 server/routes/interaction/[uid]/login.get.ts | 2 +- server/utils/discord.ts | 22 +++++++------------ 5 files changed, 10 insertions(+), 16 deletions(-) rename server/{api => routes}/discord/auth.get.ts (100%) rename server/{api => routes}/discord/callback.get.ts (100%) diff --git a/server/routes/_oauth.get.ts b/server/routes/_oauth.get.ts index c292f6e..7d97718 100644 --- a/server/routes/_oauth.get.ts +++ b/server/routes/_oauth.get.ts @@ -9,7 +9,7 @@ export default eventHandler(async (event) => { const params = new URLSearchParams({ redirect: `http://${host}/_oauth` }); return sendRedirect( event, - `${userConfig.publicUrl}/api/discord/auth?${params}` + `${userConfig.publicUrl}/discord/auth?${params}` ); } else if (code) { assert(typeof code === "string"); diff --git a/server/api/discord/auth.get.ts b/server/routes/discord/auth.get.ts similarity index 100% rename from server/api/discord/auth.get.ts rename to server/routes/discord/auth.get.ts diff --git a/server/api/discord/callback.get.ts b/server/routes/discord/callback.get.ts similarity index 100% rename from server/api/discord/callback.get.ts rename to server/routes/discord/callback.get.ts diff --git a/server/routes/interaction/[uid]/login.get.ts b/server/routes/interaction/[uid]/login.get.ts index 05a3ae4..a733780 100644 --- a/server/routes/interaction/[uid]/login.get.ts +++ b/server/routes/interaction/[uid]/login.get.ts @@ -6,5 +6,5 @@ export default eventHandler(async (event) => { assert.equal(interaction.prompt.name, "login"); const params = new URLSearchParams({ redirect: `${event.path}/../callback` }); - return sendRedirect(event, `/api/discord/auth?${params}`); + return sendRedirect(event, `/discord/auth?${params}`); }); diff --git a/server/utils/discord.ts b/server/utils/discord.ts index 1388cc2..845d22a 100644 --- a/server/utils/discord.ts +++ b/server/utils/discord.ts @@ -1,31 +1,24 @@ import { REST } from "@discordjs/rest"; import { + OAuth2Routes, RESTPostOAuth2AccessTokenResult, Routes, type RESTGetAPICurrentUserResult, type RESTGetCurrentUserGuildMemberResult, } from "discord-api-types/v10"; -const endpoints = { - authorization: "https://discord.com/oauth2/authorize", - token: "https://discord.com/api/oauth2/token", - revocation: "https://discord.com/api/oauth2/token/revoke", -}; - export function getAuthorizationUrl(params: Record = {}) { const _params = new URLSearchParams({ client_id: userConfig.discord.clientId, - redirect_uri: `${userConfig.publicUrl}/api/discord/callback`, + redirect_uri: `${userConfig.publicUrl}/discord/callback`, response_type: "code", ...params, }); - return `${endpoints.authorization}?${_params}`; + return `${OAuth2Routes.authorizationURL}?${_params}`; } -export async function exchangeCode( - code: string -): Promise { - const response = await fetch(endpoints.token, { +export async function exchangeCode(code: string) { + const resp = await fetch(OAuth2Routes.tokenURL, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", @@ -35,10 +28,11 @@ export async function exchangeCode( client_secret: userConfig.discord.clientSecret, grant_type: "authorization_code", code, - redirect_uri: `${userConfig.publicUrl}/api/discord/callback`, + redirect_uri: `${userConfig.publicUrl}/discord/callback`, }), }); - return await response.json(); + const json = await resp.json(); + return json as RESTPostOAuth2AccessTokenResult; } export async function fetchUserinfo(accessToken: string) {