mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 12:32:34 +10:00
34 lines
878 B
TypeScript
34 lines
878 B
TypeScript
import type { User } from '@prisma/client';
|
|
|
|
import { AppError } from '../../errors/app-error';
|
|
import { getBackupCodes } from './get-backup-code';
|
|
import { validateTwoFactorAuthentication } from './validate-2fa';
|
|
|
|
type ViewBackupCodesOptions = {
|
|
user: Pick<
|
|
User,
|
|
'id' | 'email' | 'twoFactorEnabled' | 'twoFactorSecret' | 'twoFactorBackupCodes'
|
|
>;
|
|
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;
|
|
};
|