Added username switching

This commit is contained in:
2023-05-09 15:23:31 +02:00
parent d2ab24ed64
commit c668ca7f37
5 changed files with 116 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
export default defineEventHandler(async (event) => {
const auth = getAuth(event)
const auth = await getAuth(event)
return auth
});

View File

@@ -0,0 +1,15 @@
import { ObjectId } from "mongodb";
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
await usersColl.updateOne({ _id: new ObjectId(auth._id) }, { $set: { username: username, useMinecraftUsername: type === 'discord' ? false : true } })
return { username: username }
});

View File

@@ -0,0 +1,23 @@
export default defineEventHandler(async (event) => {
const auth = await getAuth(event)
const userResult = await $fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `Bearer ${auth.accessToken}`
}
})
const coll = db.collection('users')
const doc = {
discord: {
id: userResult.id,
username: userResult.username,
avatarHash: userResult.avatar || null
},
}
await coll.updateOne({ 'discord.id': userResult.id }, { $set: doc })
return doc.discord
});