Integrated the updateTOTP method into the account controller

This commit is contained in:
Mathias Wagner 2023-01-21 13:25:10 +01:00
parent d9f8447dd2
commit 20cd436321
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,7 +1,7 @@
import { Account } from "../models/Account"; import { Account } from "@models/Account";
import { genSalt, hash } from "bcrypt"; import { genSalt, hash } from "bcrypt";
import { sendMail } from "@utils/email"; import { sendMail } from "@utils/email";
import { Types } from "mongoose"; import { ObjectId, Types } from "mongoose";
import { encryptClearField } from "@utils/decryption"; import { encryptClearField } from "@utils/decryption";
export const sendVerificationEmail = async (email: string, code: number, id: string) => { export const sendVerificationEmail = async (email: string, code: number, id: string) => {
@ -38,7 +38,7 @@ export const verifyAccount = async (configuration: { id: string, code: number })
if (account === null) return { code: 1002, message: "The provided account does not exist" }; 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 { if (account.verificationSecret !== configuration.code) return {
code: 1003, code: 1003,
@ -47,3 +47,16 @@ export const verifyAccount = async (configuration: { id: string, code: number })
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 });
};