Polarcraft/webv2/server/api/users/[id]/minecraft/index.put.ts
Xeovalyte ea7a3e3a8f
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m2s
Build and Deploy / Deploy Discord Bot (push) Successful in 42s
fix: Migrated from transactions to writes, closes #30
2023-06-06 10:37:30 +02:00

40 lines
1.1 KiB
TypeScript

export default defineEventHandler(async (event) => {
const { code } = await readBody(event)
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
if (!user.$isEmpty('minecraft')) {
throw createError({ statusCode: 400, statusMessage: 'Minecraft already linked' })
}
let whitelistDoc
try {
whitelistDoc = await WhitelistModel.findOne({ code })
if (!whitelistDoc) {
throw createError({ statusCode: 500, statusMessage: 'Whitelist document was not found' })
}
} catch (e) {
console.error('Failed to get whitelist document', e)
throw createError('Failed to get whitelist document')
}
const minecraftProfile: any = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${whitelistDoc.uuid}`)
try {
whitelistDoc.connected = true
user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name }
await whitelistDoc.save()
await user.save()
} catch (e) {
console.error('Failed to update documents', e)
throw createError('Failed to update documents')
}
return user.minecraft
})