2023-06-05 22:41:48 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const { code } = await readBody(event)
|
|
|
|
const userId: string = event.context.params ? event.context.params.id : '@me'
|
|
|
|
|
2023-06-06 10:37:30 +02:00
|
|
|
const user = await getUser(userId, event)
|
|
|
|
|
|
|
|
if (!user.$isEmpty('minecraft')) {
|
|
|
|
throw createError({ statusCode: 400, statusMessage: 'Minecraft already linked' })
|
|
|
|
}
|
|
|
|
|
2023-06-05 22:41:48 +02:00
|
|
|
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 }
|
|
|
|
|
2023-06-06 10:37:30 +02:00
|
|
|
await whitelistDoc.save()
|
2023-06-06 13:49:27 +02:00
|
|
|
await applyUsername(user)
|
2023-06-05 22:41:48 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Failed to update documents', e)
|
|
|
|
|
|
|
|
throw createError('Failed to update documents')
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.minecraft
|
|
|
|
})
|