2023-04-25 15:51:20 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const { code } = await readBody(event)
|
|
|
|
|
2023-05-09 16:01:19 +02:00
|
|
|
if (!code) throw createError({ statusCode: 400, statusMessage: 'Code is vereist'})
|
2023-04-25 15:51:20 +02:00
|
|
|
|
|
|
|
const auth = await getAuth(event)
|
|
|
|
|
|
|
|
const whitelistColl = db.collection('whitelist')
|
|
|
|
const whitelistDoc = await whitelistColl.findOne({ code: code.toString() })
|
|
|
|
|
2023-05-09 16:01:19 +02:00
|
|
|
if (!whitelistDoc) throw createError({ statusCode: 400, statusMessage: 'Code is niet gevonden, join eerste de Minecraft server' })
|
2023-04-25 15:51:20 +02:00
|
|
|
if (whitelistDoc && whitelistDoc.verified) throw createError({ statusCode: 400, statusMessage: 'Already verified' })
|
|
|
|
|
|
|
|
await whitelistColl.updateOne({ code: code.toString() }, { $set: { verified: true } })
|
|
|
|
|
2023-04-28 16:56:36 +02:00
|
|
|
const minecraftProfile = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${whitelistDoc.uuid}`)
|
|
|
|
|
2023-04-25 15:51:20 +02:00
|
|
|
const usersColl = db.collection('users')
|
2023-04-28 16:56:36 +02:00
|
|
|
await usersColl.updateOne({ 'discord.id': auth.discord.id }, { $set: { 'minecraft.uuid': whitelistDoc.uuid, 'minecraft.username': minecraftProfile.name } })
|
2023-04-25 15:51:20 +02:00
|
|
|
|
|
|
|
|
2023-04-28 16:56:36 +02:00
|
|
|
return { uuid: whitelistDoc.uuid, verified: false, username: minecraftProfile.name }
|
2023-04-25 15:51:20 +02:00
|
|
|
});
|