Polarcraft/webv2/server/api/users/[id]/discord.get.ts

26 lines
735 B
TypeScript
Raw Normal View History

export default defineEventHandler(async (event) => {
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
const auth = getAuth(event)
try {
const authorizationResponse: DiscordUser = await $fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `Bearer ${auth.accessToken}`
}
})
user.discord.username = authorizationResponse.global_name ?? authorizationResponse.username
user.discord.avatarHash = authorizationResponse.avatar
await applyUsername(user)
} catch (e) {
console.error('Failed to update document', e)
throw createError('Failed to update document')
}
return user.discord
})