From 20cd436321137c50c7c0a03d34f88f71ea1ae70d Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 21 Jan 2023 13:25:10 +0100 Subject: [PATCH] Integrated the updateTOTP method into the account controller --- src/controller/account.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/controller/account.ts b/src/controller/account.ts index 8b1d34d..caed0ab 100644 --- a/src/controller/account.ts +++ b/src/controller/account.ts @@ -1,7 +1,7 @@ -import { Account } from "../models/Account"; +import { Account } from "@models/Account"; import { genSalt, hash } from "bcrypt"; import { sendMail } from "@utils/email"; -import { Types } from "mongoose"; +import { ObjectId, Types } from "mongoose"; import { encryptClearField } from "@utils/decryption"; export const sendVerificationEmail = async (email: string, code: number, id: string) => { @@ -38,12 +38,25 @@ export const verifyAccount = async (configuration: { id: string, code: number }) if (account === null) return { code: 1002, message: "The provided account does not exist" }; - if (account.verified) return { code: 1003, message: "The provided account is already verified" } + if (account.verified) return { code: 1003, message: "The provided account is already verified" }; if (account.verificationSecret !== configuration.code) return { code: 1003, message: "The provided verification secret is wrong", }; - await Account.findByIdAndUpdate(configuration.id, {verified: true, $unset: {verificationSecret: 1}}); + await Account.findByIdAndUpdate(configuration.id, { verified: true, $unset: { verificationSecret: 1 } }); +}; + +export const updateTOTP = async (id: ObjectId | undefined, status: boolean) => { + const account = await Account.findById(id); + + if (account === null) return { code: 1002, message: "The provided account does not exist" }; + + if (account.totpEnabled === status) return { + code: 1009, + message: `TOTP is already ${status ? "enabled" : "disabled"} on your account`, + }; + + await Account.findByIdAndUpdate(id, { totpEnabled: status }); }; \ No newline at end of file