diff --git a/web/pages/whitelist.vue b/web/pages/whitelist.vue index 447b4f0..3a3259a 100644 --- a/web/pages/whitelist.vue +++ b/web/pages/whitelist.vue @@ -1,12 +1,31 @@ @@ -19,6 +38,34 @@ definePageMeta({ const user = useState('user') const code = ref('') +const refreshUsername = async () => { + try { + const response = await $fetch('/api/minecraft/refreshusername') + + user.value.minecraft.username = response.username + + useToast().success('Username is ververst') + } catch (e) { + console.log(e) + + useToast().error(e.statusMessage) + } +} + +const removeWhitelist = async () => { + try { + await $fetch('/api/minecraft/removewhitelist') + + user.value.minecraft.uuid = null + user.value.minecraft.username = null + + useToast().success('Minecraft is niet meer gekoppeld') + } catch (e) { + console.log(e) + useToast().error(e.statusMessage) + } +} + const submitCode = async () => { try { const response = await $fetch('/api/minecraft/whitelist', { @@ -29,6 +76,8 @@ const submitCode = async () => { }) user.value.minecraft.uuid = response.uuid + user.value.minecraft.username = response.username + useToast().success('Successfully whitelisted') } catch (e) { console.log(e); diff --git a/web/server/api/minecraft/refreshusername.js b/web/server/api/minecraft/refreshusername.js new file mode 100644 index 0000000..c1b93f5 --- /dev/null +++ b/web/server/api/minecraft/refreshusername.js @@ -0,0 +1,12 @@ +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 } +}); diff --git a/web/server/api/minecraft/removewhitelist.js b/web/server/api/minecraft/removewhitelist.js new file mode 100644 index 0000000..29fbafa --- /dev/null +++ b/web/server/api/minecraft/removewhitelist.js @@ -0,0 +1,13 @@ +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 whitelistColl = db.collection('whitelist') + await whitelistColl.deleteOne({ uuid: auth.minecraft.uuid }) + + const usersColl = db.collection('users') + await usersColl.findOneAndUpdate({ 'minecraft.uuid': auth.minecraft.uuid }, { $set: { 'minecraft.uuid': null, 'minecraft.username': null } }) + + return { code: 'success' } +}); diff --git a/web/server/api/minecraft/verifyuuid.js b/web/server/api/minecraft/verifyuuid.js index e771a45..7db5bf5 100644 --- a/web/server/api/minecraft/verifyuuid.js +++ b/web/server/api/minecraft/verifyuuid.js @@ -1,6 +1,4 @@ export default defineEventHandler(async (event) => { - const auth = getAuth(event) - const { uuid } = await readBody(event) const coll = db.collection('whitelist') diff --git a/web/server/api/minecraft/whitelist.js b/web/server/api/minecraft/whitelist.js index 98c9082..d6a89f0 100644 --- a/web/server/api/minecraft/whitelist.js +++ b/web/server/api/minecraft/whitelist.js @@ -3,7 +3,6 @@ export default defineEventHandler(async (event) => { if (!code) throw createError({ statusCode: 400, statusMessage: 'Code is required'}) - const config = useRuntimeConfig() const auth = await getAuth(event) const whitelistColl = db.collection('whitelist') @@ -14,9 +13,11 @@ export default defineEventHandler(async (event) => { await whitelistColl.updateOne({ code: code.toString() }, { $set: { verified: true } }) + const minecraftProfile = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${whitelistDoc.uuid}`) + const usersColl = db.collection('users') - await usersColl.updateOne({ 'discord.id': auth.discord.id }, { $set: { 'minecraft.uuid': whitelistDoc.uuid } }) + await usersColl.updateOne({ 'discord.id': auth.discord.id }, { $set: { 'minecraft.uuid': whitelistDoc.uuid, 'minecraft.username': minecraftProfile.name } }) - return { uuid: whitelistDoc.uuid, verified: false } + return { uuid: whitelistDoc.uuid, verified: false, username: minecraftProfile.name } });