2024-03-25 11:34:50 +08:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
|
|
import { useRouter } from 'next/navigation';
|
2023-12-01 05:52:16 +05:30
|
|
|
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { renderSVG } from 'uqr';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
2024-02-21 02:19:35 +00:00
|
|
|
import { downloadFile } from '@documenso/lib/client-only/download-file';
|
2023-12-01 05:52:16 +05:30
|
|
|
import { trpc } from '@documenso/trpc/react';
|
|
|
|
|
import { Button } from '@documenso/ui/primitives/button';
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
2024-03-25 11:34:50 +08:00
|
|
|
DialogClose,
|
2023-12-01 05:52:16 +05:30
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
2024-01-30 17:31:27 +11:00
|
|
|
DialogFooter,
|
2023-12-01 05:52:16 +05:30
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
2024-03-25 11:34:50 +08:00
|
|
|
DialogTrigger,
|
2023-12-01 05:52:16 +05:30
|
|
|
} from '@documenso/ui/primitives/dialog';
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from '@documenso/ui/primitives/form/form';
|
2024-05-24 03:31:19 +00:00
|
|
|
import { PinInput, PinInputGroup, PinInputSlot } from '@documenso/ui/primitives/pin-input';
|
2023-12-01 05:52:16 +05:30
|
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
|
|
|
|
|
|
|
|
import { RecoveryCodeList } from './recovery-code-list';
|
|
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
export const ZEnable2FAForm = z.object({
|
2023-12-01 05:52:16 +05:30
|
|
|
token: z.string(),
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
export type TEnable2FAForm = z.infer<typeof ZEnable2FAForm>;
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-31 15:49:12 +08:00
|
|
|
export type EnableAuthenticatorAppDialogProps = {
|
|
|
|
|
onSuccess?: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const EnableAuthenticatorAppDialog = ({ onSuccess }: EnableAuthenticatorAppDialogProps) => {
|
2023-12-01 05:52:16 +05:30
|
|
|
const { toast } = useToast();
|
2024-03-31 15:49:12 +08:00
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const router = useRouter();
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
|
const [recoveryCodes, setRecoveryCodes] = useState<string[] | null>(null);
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const { mutateAsync: enable2FA } = trpc.twoFactorAuthentication.enable.useMutation();
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const {
|
|
|
|
|
mutateAsync: setup2FA,
|
|
|
|
|
data: setup2FAData,
|
|
|
|
|
isLoading: isSettingUp2FA,
|
|
|
|
|
} = trpc.twoFactorAuthentication.setup.useMutation({
|
|
|
|
|
onError: () => {
|
|
|
|
|
toast({
|
|
|
|
|
title: 'Unable to setup two-factor authentication',
|
|
|
|
|
description:
|
|
|
|
|
'We were unable to setup two-factor authentication for your account. Please ensure that you have entered your code correctly and try again.',
|
|
|
|
|
variant: 'destructive',
|
|
|
|
|
});
|
2023-12-01 05:52:16 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const enable2FAForm = useForm<TEnable2FAForm>({
|
2023-12-01 05:52:16 +05:30
|
|
|
defaultValues: {
|
|
|
|
|
token: '',
|
|
|
|
|
},
|
2024-03-25 11:34:50 +08:00
|
|
|
resolver: zodResolver(ZEnable2FAForm),
|
2023-12-01 05:52:16 +05:30
|
|
|
});
|
|
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const { isSubmitting: isEnabling2FA } = enable2FAForm.formState;
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const onEnable2FAFormSubmit = async ({ token }: TEnable2FAForm) => {
|
|
|
|
|
try {
|
|
|
|
|
const data = await enable2FA({ code: token });
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
setRecoveryCodes(data.recoveryCodes);
|
2024-03-31 15:49:12 +08:00
|
|
|
onSuccess?.();
|
2023-12-01 05:52:16 +05:30
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
toast({
|
|
|
|
|
title: 'Two-factor authentication enabled',
|
|
|
|
|
description:
|
|
|
|
|
'You will now be required to enter a code from your authenticator app when signing in.',
|
|
|
|
|
});
|
2023-12-01 05:52:16 +05:30
|
|
|
} catch (_err) {
|
|
|
|
|
toast({
|
|
|
|
|
title: 'Unable to setup two-factor authentication',
|
|
|
|
|
description:
|
2024-03-31 15:49:12 +08:00
|
|
|
'We were unable to setup two-factor authentication for your account. Please ensure that you have entered your code correctly and try again.',
|
2023-12-01 05:52:16 +05:30
|
|
|
variant: 'destructive',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-19 11:25:15 +00:00
|
|
|
const downloadRecoveryCodes = () => {
|
2024-03-25 11:34:50 +08:00
|
|
|
if (recoveryCodes) {
|
|
|
|
|
const blob = new Blob([recoveryCodes.join('\n')], {
|
2024-02-19 11:25:15 +00:00
|
|
|
type: 'text/plain',
|
|
|
|
|
});
|
2024-02-21 02:19:35 +00:00
|
|
|
|
|
|
|
|
downloadFile({
|
|
|
|
|
filename: 'documenso-2FA-recovery-codes.txt',
|
|
|
|
|
data: blob,
|
|
|
|
|
});
|
2024-02-19 11:25:15 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-25 11:34:50 +08:00
|
|
|
const handleEnable2FA = async () => {
|
|
|
|
|
if (!setup2FAData) {
|
|
|
|
|
await setup2FA();
|
2023-12-01 05:52:16 +05:30
|
|
|
}
|
2024-03-25 11:34:50 +08:00
|
|
|
|
|
|
|
|
setIsOpen(true);
|
2023-12-01 05:52:16 +05:30
|
|
|
};
|
|
|
|
|
|
2024-03-10 09:36:54 +00:00
|
|
|
useEffect(() => {
|
2024-03-25 11:34:50 +08:00
|
|
|
enable2FAForm.reset();
|
|
|
|
|
|
|
|
|
|
if (!isOpen && recoveryCodes && recoveryCodes.length > 0) {
|
|
|
|
|
setRecoveryCodes(null);
|
|
|
|
|
router.refresh();
|
2024-03-11 10:55:46 +11:00
|
|
|
}
|
2024-03-25 11:34:50 +08:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [isOpen]);
|
2024-03-10 09:36:54 +00:00
|
|
|
|
2023-12-01 05:52:16 +05:30
|
|
|
return (
|
2024-03-25 11:34:50 +08:00
|
|
|
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
|
|
|
|
<DialogTrigger asChild={true}>
|
|
|
|
|
<Button
|
|
|
|
|
className="flex-shrink-0"
|
|
|
|
|
loading={isSettingUp2FA}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
void handleEnable2FA();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Enable 2FA
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
|
|
|
|
|
<DialogContent position="center">
|
|
|
|
|
{setup2FAData && (
|
|
|
|
|
<>
|
|
|
|
|
{recoveryCodes ? (
|
|
|
|
|
<div>
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Backup codes</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
Your recovery codes are listed below. Please store them in a safe place.
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
<div className="mt-4">
|
|
|
|
|
<RecoveryCodeList recoveryCodes={recoveryCodes} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<DialogFooter className="mt-4">
|
|
|
|
|
<DialogClose asChild>
|
|
|
|
|
<Button variant="secondary">Close</Button>
|
|
|
|
|
</DialogClose>
|
|
|
|
|
|
|
|
|
|
<Button onClick={downloadRecoveryCodes}>Download</Button>
|
2024-01-30 17:31:27 +11:00
|
|
|
</DialogFooter>
|
2023-12-01 05:52:16 +05:30
|
|
|
</div>
|
2024-03-25 11:34:50 +08:00
|
|
|
) : (
|
|
|
|
|
<Form {...enable2FAForm}>
|
|
|
|
|
<form onSubmit={enable2FAForm.handleSubmit(onEnable2FAFormSubmit)}>
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Enable Authenticator App</DialogTitle>
|
|
|
|
|
<DialogDescription>
|
|
|
|
|
To enable two-factor authentication, scan the following QR code using your
|
|
|
|
|
authenticator app.
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
|
|
|
|
<fieldset disabled={isEnabling2FA} className="mt-4 flex flex-col gap-y-4">
|
|
|
|
|
<div
|
|
|
|
|
className="flex h-36 justify-center"
|
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
|
__html: renderSVG(setup2FAData?.uri ?? ''),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<p className="text-muted-foreground text-sm">
|
|
|
|
|
If your authenticator app does not support QR codes, you can use the following
|
|
|
|
|
code instead:
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p className="bg-muted/60 text-muted-foreground rounded-lg p-2 text-center font-mono tracking-widest">
|
|
|
|
|
{setup2FAData?.secret}
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p className="text-muted-foreground text-sm">
|
|
|
|
|
Once you have scanned the QR code or entered the code manually, enter the code
|
|
|
|
|
provided by your authenticator app below.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
name="token"
|
|
|
|
|
control={enable2FAForm.control}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel className="text-muted-foreground">Token</FormLabel>
|
|
|
|
|
<FormControl>
|
2024-05-24 03:31:19 +00:00
|
|
|
<PinInput {...field} value={field.value ?? ''} maxLength={6}>
|
|
|
|
|
{Array(6)
|
|
|
|
|
.fill(null)
|
|
|
|
|
.map((_, i) => (
|
|
|
|
|
<PinInputGroup key={i}>
|
|
|
|
|
<PinInputSlot index={i} />
|
|
|
|
|
</PinInputGroup>
|
|
|
|
|
))}
|
|
|
|
|
</PinInput>
|
2024-03-25 11:34:50 +08:00
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<DialogFooter>
|
|
|
|
|
<DialogClose asChild>
|
|
|
|
|
<Button variant="secondary">Cancel</Button>
|
|
|
|
|
</DialogClose>
|
|
|
|
|
|
|
|
|
|
<Button type="submit" loading={isEnabling2FA}>
|
|
|
|
|
Enable 2FA
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</fieldset>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2023-12-01 05:52:16 +05:30
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|