Polarcraft/web/server/api/minecraft/whitelist.js

24 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-04-25 15:51:20 +02:00
export default defineEventHandler(async (event) => {
const { code } = await readBody(event)
if (!code) throw createError({ statusCode: 400, statusMessage: 'Code is required'})
const auth = await getAuth(event)
const whitelistColl = db.collection('whitelist')
const whitelistDoc = await whitelistColl.findOne({ code: code.toString() })
if (!whitelistDoc) throw createError({ statusCode: 400, statusMessage: 'Code has not been found, join the server first' })
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
});