summary refs log tree commit diff
path: root/src/api/routes/users/@me/mfa/codes-verification.ts
blob: 570606e0e84093431dc14e0c70cd7b7f6592eb14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { route } from "@fosscord/api";
import { BackupCode, CodesVerificationSchema, generateMfaBackupCodes, User } from "@fosscord/util";
import { Request, Response, Router } from "express";

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;