Polarcraft/web/server/api/minecraft/refreshusername.js
Xeovalyte 8121b9b975
Some checks failed
Build and Deploy / Deploy Web (push) Has been cancelled
Build and Deploy / Deploy Discord Bot (push) Has been cancelled
Lots of improvements and changes
2023-05-27 12:02:33 +02:00

15 lines
712 B
JavaScript

export default defineEventHandler(async (event) => {
const user = await getAuth(event)
if (!auth.minecraft.uuid) throw createError({ errorCode: 400, statusMessage: 'Geen Minecraft account is gekoppeld' })
const minecraftProfile = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${user.minecraft.uuid}`)
const usersColl = db.collection('users')
const newUser = await usersColl.findOneAndUpdate({ 'minecraft.uuid': auth.minecraft.uuid }, { $set: { 'minecraft.username': minecraftProfile.name, username: user.useMinecraftUsername ? minecraftProfile.name : user.discord.username } }, { returnDocument: 'after' })
applyUsername(newUser.value)
return newUser.value
});