mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
feat: ability to download all the 2FA recovery codes
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { useMemo } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
@ -41,6 +41,7 @@ export type ViewRecoveryCodesDialogProps = {
|
|||||||
|
|
||||||
export const ViewRecoveryCodesDialog = ({ open, onOpenChange }: ViewRecoveryCodesDialogProps) => {
|
export const ViewRecoveryCodesDialog = ({ open, onOpenChange }: ViewRecoveryCodesDialogProps) => {
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
const [recoveryCodesUrl, setRecoveryCodesUrl] = useState('');
|
||||||
|
|
||||||
const { mutateAsync: viewRecoveryCodes, data: viewRecoveryCodesData } =
|
const { mutateAsync: viewRecoveryCodes, data: viewRecoveryCodesData } =
|
||||||
trpc.twoFactorAuthentication.viewRecoveryCodes.useMutation();
|
trpc.twoFactorAuthentication.viewRecoveryCodes.useMutation();
|
||||||
@ -62,6 +63,16 @@ export const ViewRecoveryCodesDialog = ({ open, onOpenChange }: ViewRecoveryCode
|
|||||||
return 'view';
|
return 'view';
|
||||||
}, [viewRecoveryCodesData, isViewRecoveryCodesSubmitting]);
|
}, [viewRecoveryCodesData, isViewRecoveryCodesSubmitting]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (viewRecoveryCodesData && viewRecoveryCodesData.recoveryCodes) {
|
||||||
|
const textBlob = new Blob([viewRecoveryCodesData.recoveryCodes.join('\n')], {
|
||||||
|
type: 'text/plain',
|
||||||
|
});
|
||||||
|
if (recoveryCodesUrl) URL.revokeObjectURL(recoveryCodesUrl);
|
||||||
|
setRecoveryCodesUrl(URL.createObjectURL(textBlob));
|
||||||
|
}
|
||||||
|
}, [viewRecoveryCodesData]);
|
||||||
|
|
||||||
const onViewRecoveryCodesFormSubmit = async ({ password }: TViewRecoveryCodesForm) => {
|
const onViewRecoveryCodesFormSubmit = async ({ password }: TViewRecoveryCodesForm) => {
|
||||||
try {
|
try {
|
||||||
await viewRecoveryCodes({ password });
|
await viewRecoveryCodes({ password });
|
||||||
@ -139,8 +150,11 @@ export const ViewRecoveryCodesDialog = ({ open, onOpenChange }: ViewRecoveryCode
|
|||||||
<RecoveryCodeList recoveryCodes={viewRecoveryCodesData.recoveryCodes} />
|
<RecoveryCodeList recoveryCodes={viewRecoveryCodesData.recoveryCodes} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="mt-4 flex flex-row-reverse items-center justify-between">
|
<div className="mt-4 flex flex-row-reverse items-center gap-2">
|
||||||
<Button onClick={() => onOpenChange(false)}>Complete</Button>
|
<Button onClick={() => onOpenChange(false)}>Complete</Button>
|
||||||
|
<a download="documenso-2FA-recovery-codes.txt" href={recoveryCodesUrl}>
|
||||||
|
<Button>Download</Button>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))
|
))
|
||||||
|
|||||||
Reference in New Issue
Block a user