mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-07 08:26:37 -05:00
feat: add connections route for current user
This commit is contained in:
28
server/api/auth/me-connections.get.ts
Normal file
28
server/api/auth/me-connections.get.ts
Normal file
@@ -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<GetApiAuthMeConections> => {
|
||||
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
|
||||
};
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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: <span v-if="connections?.discord">
|
||||
<img
|
||||
:style="{ height: '25px', width: '25px', borderRadius: '100px'}"
|
||||
:src="connections.discord.avatarUrl ?? '#'"
|
||||
>
|
||||
{{ connections.discord.username }}
|
||||
</span>
|
||||
</button>
|
||||
<button
|
||||
v-else
|
||||
|
||||
Reference in New Issue
Block a user