mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
31 lines
788 B
TypeScript
31 lines
788 B
TypeScript
import type { User } from '@documenso/prisma/client';
|
|
|
|
import { AppError } from '../../errors/app-error';
|
|
import { getBackupCodes } from './get-backup-code';
|
|
import { validateTwoFactorAuthentication } from './validate-2fa';
|
|
|
|
type ViewBackupCodesOptions = {
|
|
user: User;
|
|
token: string;
|
|
};
|
|
|
|
export const viewBackupCodes = async ({ token, user }: ViewBackupCodesOptions) => {
|
|
let isValid = await validateTwoFactorAuthentication({ totpCode: token, user });
|
|
|
|
if (!isValid) {
|
|
isValid = await validateTwoFactorAuthentication({ backupCode: token, user });
|
|
}
|
|
|
|
if (!isValid) {
|
|
throw new AppError('INCORRECT_TWO_FACTOR_CODE');
|
|
}
|
|
|
|
const backupCodes = getBackupCodes({ user });
|
|
|
|
if (!backupCodes) {
|
|
throw new AppError('MISSING_BACKUP_CODE');
|
|
}
|
|
|
|
return backupCodes;
|
|
};
|