2023-05-09 15:23:31 +02:00
|
|
|
import { ObjectId } from "mongodb";
|
2023-05-11 19:46:18 +02:00
|
|
|
import { applyUsername } from "~/server/utils/auth";
|
2023-05-09 15:23:31 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-11 19:46:18 +02:00
|
|
|
const newUser = await usersColl.findOneAndUpdate({ _id: new ObjectId(auth._id) }, { $set: { username: username, useMinecraftUsername: type === 'discord' ? false : true } }, { returnDocument: 'after' })
|
|
|
|
await applyUsername(newUser.value)
|
2023-05-09 15:23:31 +02:00
|
|
|
|
2023-05-11 19:46:18 +02:00
|
|
|
return newUser.value
|
2023-05-09 15:23:31 +02:00
|
|
|
});
|