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

13 lines
566 B
JavaScript
Raw Normal View History

2023-04-28 16:56:36 +02:00
export default defineEventHandler(async (event) => {
const auth = await getAuth(event)
if (!auth.minecraft.uuid) throw createError({ errorCode: 400, statusMessage: 'No Minecraft account is linked' })
const minecraftProfile = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${auth.minecraft.uuid}`)
const usersColl = db.collection('users')
await usersColl.findOneAndUpdate({ 'minecraft.uuid': auth.minecraft.uuid }, { $set: { 'minecraft.username': minecraftProfile.name } })
return { username: minecraftProfile.name }
});