Polarcraft/web/server/api/auth/user/setdefaultusername.js
2023-05-11 19:46:18 +02:00

17 lines
623 B
JavaScript

import { ObjectId } from "mongodb";
import { applyUsername } from "~/server/utils/auth";
export default defineEventHandler(async (event) => {
const { type } = await readBody(event)
const auth = await getAuth(event)
const usersColl = db.collection('users')
const username = type === 'discord' ? auth.discord.username : auth.minecraft.username
const newUser = await usersColl.findOneAndUpdate({ _id: new ObjectId(auth._id) }, { $set: { username: username, useMinecraftUsername: type === 'discord' ? false : true } }, { returnDocument: 'after' })
await applyUsername(newUser.value)
return newUser.value
});