summary refs log tree commit diff
path: root/src/api/routes/users/@me/mfa/codes-verification.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-04 15:47:19 +1000
committerGitHub <noreply@github.com>2022-09-04 15:47:19 +1000
commit6977283e0e771ab1f5eae3f916bf56077aa61178 (patch)
tree890bee6f677ab73a1621f1c281f3f391e3b7b3a3 /src/api/routes/users/@me/mfa/codes-verification.ts
parentMerge pull request #879 from Puyodead1/fix/user-profiles (diff)
downloadserver-6977283e0e771ab1f5eae3f916bf56077aa61178.tar.xz
Various fixes for current web client (#875)
* Placeholder member verification endpoint

* Welcome screen

* Fix types for welcome screen

* Try to allow role icons to be removed

* Fix typo in genschemas script

* Remove left over welcome-screen endpoint

* Embedded activities
Diffstat (limited to 'src/api/routes/users/@me/mfa/codes-verification.ts')
-rw-r--r--src/api/routes/users/@me/mfa/codes-verification.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/api/routes/users/@me/mfa/codes-verification.ts b/src/api/routes/users/@me/mfa/codes-verification.ts
new file mode 100644
index 00000000..071c71fa
--- /dev/null
+++ b/src/api/routes/users/@me/mfa/codes-verification.ts
@@ -0,0 +1,41 @@
+import { Router, Request, Response } from "express";
+import { route } from "@fosscord/api";
+import { BackupCode, generateMfaBackupCodes, User, CodesVerificationSchema } from "@fosscord/util";
+
+const router = Router();
+
+router.post("/", route({ body: "CodesVerificationSchema" }), async (req: Request, res: Response) => {
+	const { key, nonce, regenerate } = req.body as CodesVerificationSchema;
+
+	// TODO: We don't have email/etc etc, so can't send a verification code.
+	// Once that's done, this route can verify `key`
+
+	const user = await User.findOneOrFail({ where: { id: req.user_id } });
+
+	var codes: BackupCode[];
+	if (regenerate) {
+		await BackupCode.update(
+			{ user: { id: req.user_id } },
+			{ expired: true }
+		);
+
+		codes = generateMfaBackupCodes(req.user_id);
+		await Promise.all(codes.map(x => x.save()));
+	}
+	else {
+		codes = await BackupCode.find({
+			where: {
+				user: {
+					id: req.user_id,
+				},
+				expired: false,
+			}
+		});
+	}
+
+	return res.json({
+		backup_codes: codes.map(x => ({ ...x, expired: undefined })),
+	});
+});
+
+export default router;