Skip to content

Commit

Permalink
move /api/discord, unhardcode endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Apr 10, 2024
1 parent b38d51e commit c684a46
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion server/routes/_oauth.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion server/routes/interaction/[uid]/login.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
22 changes: 8 additions & 14 deletions server/utils/discord.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {}) {
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<RESTPostOAuth2AccessTokenResult> {
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",
Expand All @@ -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) {
Expand Down

0 comments on commit c684a46

Please sign in to comment.