Polarcraft/web/server/api/minecraft/refreshusername.js
2023-05-11 19:46:18 +02:00

17 lines
766 B
JavaScript

import { applyUsername } from "~/server/utils/auth";
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
});