Polarcraft/webv2/server/api/users/[id]/minecraft.get.ts
Xeovalyte 3eb4453d71
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m5s
Build and Deploy / Deploy Discord Bot (push) Successful in 44s
feat: Added minecraft linking system, closes #29
2023-06-05 22:41:48 +02:00

24 lines
690 B
TypeScript

export default defineEventHandler(async (event) => {
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
if (!user.minecraft) {
throw createError({ statusCode: 400, statusMessage: 'Minecraft has not been linked' })
}
const minecraftProfile: any = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${user.minecraft.uuid}`)
try {
user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }
user.save()
} catch (e) {
console.error('Failed to update document', e)
throw createError('Failed to update document')
}
return user.minecraft
})