From d74a51db7f9623cbd8e1a5860e523890241ab5e3 Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Mon, 5 Jun 2023 16:05:03 +0200 Subject: [PATCH] feat: Added verify account route, closes #28 --- webv2/middleware/{auth.js => auth.ts} | 0 .../server/api/minecraft/verifyaccount.get.ts | 24 +++++++++++++++++++ webv2/server/utils/models.ts | 7 ++++++ webv2/types/global.d.ts | 7 ++++++ 4 files changed, 38 insertions(+) rename webv2/middleware/{auth.js => auth.ts} (100%) create mode 100644 webv2/server/api/minecraft/verifyaccount.get.ts diff --git a/webv2/middleware/auth.js b/webv2/middleware/auth.ts similarity index 100% rename from webv2/middleware/auth.js rename to webv2/middleware/auth.ts diff --git a/webv2/server/api/minecraft/verifyaccount.get.ts b/webv2/server/api/minecraft/verifyaccount.get.ts new file mode 100644 index 0000000..5bc6a8d --- /dev/null +++ b/webv2/server/api/minecraft/verifyaccount.get.ts @@ -0,0 +1,24 @@ +export default defineEventHandler(async (event) => { + const { uuid } = getQuery(event) + + let whitelistDoc + try { + whitelistDoc = await WhitelistModel.findOneAndUpdate({ uuid }, { + $set: { + uuid + }, + $setOnInsert: { + code: generateCode(), + connected: false + } + }, { returnDocument: 'after', upsert: true }) + + return whitelistDoc + } catch (e: any) { + throw createError({ statusCode: 500, statusMessage: 'Failed to update/insert whitelist document' }) + } +}) + +const generateCode = () => { + return Math.floor(100000 + Math.random() * 900000) +} diff --git a/webv2/server/utils/models.ts b/webv2/server/utils/models.ts index e31048e..d64ae4e 100644 --- a/webv2/server/utils/models.ts +++ b/webv2/server/utils/models.ts @@ -21,4 +21,11 @@ const userSchema = new Schema({ ] }) +const whitelistSchema = new Schema({ + uuid: { type: String, required: true, unique: true }, + connected: { type: Boolean, required: true, default: false }, + code: { type: String, required: true, unique: true, length: 6 } +}) + export const UserModel = model('User', userSchema) +export const WhitelistModel = model('Whitelist', whitelistSchema) diff --git a/webv2/types/global.d.ts b/webv2/types/global.d.ts index 98a452d..2175ac9 100644 --- a/webv2/types/global.d.ts +++ b/webv2/types/global.d.ts @@ -21,4 +21,11 @@ declare global { teamId: string, accessToken?: string } + + interface IWhitelist { + _id: string, + uuid: string, + connected: boolean, + code: string + } }