Polarcraft/web/server/api/minecraft/verifyuuid.js

39 lines
1.1 KiB
JavaScript

import { getUsernameWithTeam } from "~/server/utils/auth";
export default defineEventHandler(async (event) => {
const { uuid } = await readBody(event)
const coll = db.collection('whitelist')
const usersColl = db.collection('users')
const doc = await coll.findOne({ uuid: uuid })
if (doc && !doc.verified) return { code: doc.code, verified: false }
if (doc && doc.verified) {
const user = await usersColl.findOne({ 'minecraft.uuid': uuid });
const response = await getUsernameWithTeam(user);
return { verified: true, username: response.username, usernameWithoutStyle: response.usernameWithoutStyle }
}
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;
}
}