From 5a890773027a687cb1b8f471cea928c09384f3fe Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sat, 15 Aug 2026 00:03:21 +0200 Subject: [PATCH] feat: add connections route for current user --- server/api/auth/me-connections.get.ts | 28 +++++++++++++++++++++++++++ server/utils/discord.ts | 11 +++++++++++ shared/api-types.ts | 11 +++++++++++ src/pages/account/index.vue | 10 +++++++++- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 server/api/auth/me-connections.get.ts diff --git a/server/api/auth/me-connections.get.ts b/server/api/auth/me-connections.get.ts new file mode 100644 index 0000000..1499cfc --- /dev/null +++ b/server/api/auth/me-connections.get.ts @@ -0,0 +1,28 @@ +import { getDiscordUser } from '~~/server/utils/discord'; +import type { GetApiAuthMeConections } from '~~/shared/api-types'; +import type { DiscordUser } from '~~/server/utils/discord'; + +export default defineEventHandler(async (event): Promise => { + const auth = enforceLoggedIn(event); + const discord = useDiscord(event); + + const apiGrpc = useApiGrpcWithToken(event, auth.token); + const data = await apiGrpc.getUserData({}); + let discordUser: DiscordUser | null = null; + if (data.connections?.discord?.id && discord) { + discordUser = await getDiscordUser(discord, data.connections.discord.id); + } + + return { + pid: data.pid, + discord: discordUser + ? { + id: discordUser.id, + username: discordUser.username, + discriminator: discordUser.discriminator, + avatar: discordUser.avatar ?? null, + avatarUrl: discordUser.avatar ? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.png` : null + } + : null + }; +}); diff --git a/server/utils/discord.ts b/server/utils/discord.ts index 0e36fe0..8f899ba 100644 --- a/server/utils/discord.ts +++ b/server/utils/discord.ts @@ -2,6 +2,13 @@ import { REST } from '@discordjs/rest'; import { Routes } from 'discord-api-types/v10'; import type { H3Event } from 'h3'; +export type DiscordUser = { + id: string; + username: string; + discriminator: string; + avatar?: string; +}; + type DiscordIds = { guildId: string; supporterRoleId: string | null; testerRoleId: string | null }; type DiscordInstance = { @@ -68,3 +75,7 @@ export async function removeDiscordMemberTesterRole(discord: DiscordInstance, me await discord.rest.delete(Routes.guildMemberRole(discord.ids.guildId, memberId, discord.ids.testerRoleId)); } } + +export async function getDiscordUser(discord: DiscordInstance, userId: string) { + return await discord.rest.get(Routes.user(userId)) as DiscordUser; +} diff --git a/shared/api-types.ts b/shared/api-types.ts index 16dc14e..0eecc43 100644 --- a/shared/api-types.ts +++ b/shared/api-types.ts @@ -18,6 +18,17 @@ export type GetApiAuthMe = { } | null; }; +export type GetApiAuthMeConections = { + pid: number; + discord: { + id: string; + username: string; + discriminator: string; + avatar: string | null; + avatarUrl: string | null; + } | null; +}; + export type ProgressItem = { title: string; githubUrl?: string; diff --git a/src/pages/account/index.vue b/src/pages/account/index.vue index 7c81bd9..a92c3c1 100644 --- a/src/pages/account/index.vue +++ b/src/pages/account/index.vue @@ -7,6 +7,7 @@ definePageMeta({ const authStore = useAuthStore(); const { data: profile, refresh } = await useApiFetch('/api/auth/me'); +const { data: connections, refresh: refreshConnections } = await useApiFetch('/api/auth/me-connections'); async function updateServerEnvironment(env: ApiAccountUpdateRequest['environment']) { try { @@ -54,6 +55,7 @@ async function unlinkDiscord() { method: 'POST' }); await refresh(); + await refreshConnections(); } catch (error: unknown) { const err = getApiError(error); alert(err.code); @@ -96,7 +98,13 @@ async function updateMii() { v-if="profile.discordId" @click="unlinkDiscord()" > - Unlink {{ profile.discordId }} discord + Unlink discord: + + {{ connections.discord.username }} +