added beginning of whitelist system

This commit is contained in:
2023-04-25 15:51:20 +02:00
parent fa0a9b8e58
commit 796b156e8d
18 changed files with 421 additions and 80 deletions

View File

@@ -0,0 +1,30 @@
export default defineEventHandler(async (event) => {
const auth = getAuth(event)
const { uuid } = await readBody(event)
const coll = db.collection('whitelist')
const doc = await coll.findOne({ uuid: uuid })
if (doc && !doc.verified) return { code: doc.code, verified: false }
if (doc && doc.verified) return { verified: true }
await coll.createIndex({ code: 1 }, { unique: true })
const code = await insertDoc(coll, uuid)
return { code: code.toString(), verified: false }
});
const insertDoc = async (coll, uuid) => {
try {
const code = Math.floor(100000 + Math.random() * 900000)
await coll.insertOne({ uuid: uuid, verified: false, code: code.toString() })
return code;
} catch (e) {
const code = await insertDoc(coll, uuid)
return code;
}
}

View File

@@ -0,0 +1,22 @@
export default defineEventHandler(async (event) => {
const { code } = await readBody(event)
if (!code) throw createError({ statusCode: 400, statusMessage: 'Code is required'})
const config = useRuntimeConfig()
const auth = await getAuth(event)
const whitelistColl = db.collection('whitelist')
const whitelistDoc = await whitelistColl.findOne({ code: code.toString() })
if (!whitelistDoc) throw createError({ statusCode: 400, statusMessage: 'Code has not been found, join the server first' })
if (whitelistDoc && whitelistDoc.verified) throw createError({ statusCode: 400, statusMessage: 'Already verified' })
await whitelistColl.updateOne({ code: code.toString() }, { $set: { verified: true } })
const usersColl = db.collection('users')
await usersColl.updateOne({ 'discord.id': auth.discord.id }, { $set: { 'minecraft.uuid': whitelistDoc.uuid } })
return { uuid: whitelistDoc.uuid, verified: false }
});