2024-03-25 11:34:50 +08:00
'use client' ;
import { useState } from 'react' ;
2023-12-01 05:52:16 +05:30
import { useRouter } from 'next/navigation' ;
import { zodResolver } from '@hookform/resolvers/zod' ;
import { flushSync } from 'react-dom' ;
import { useForm } from 'react-hook-form' ;
import { z } from 'zod' ;
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 ,
FormMessage ,
} from '@documenso/ui/primitives/form/form' ;
import { Input } from '@documenso/ui/primitives/input' ;
import { useToast } from '@documenso/ui/primitives/use-toast' ;
2024-03-25 11:34:50 +08:00
export const ZDisable2FAForm = z . object ( {
token : z.string ( ) ,
2023-12-01 05:52:16 +05:30
} ) ;
2024-03-25 11:34:50 +08:00
export type TDisable2FAForm = z . infer < typeof ZDisable2FAForm > ;
2023-12-01 05:52:16 +05:30
2024-03-25 11:34:50 +08:00
export const DisableAuthenticatorAppDialog = ( ) = > {
2023-12-01 05:52:16 +05:30
const router = useRouter ( ) ;
2024-03-25 11:34:50 +08:00
2023-12-01 05:52:16 +05:30
const { toast } = useToast ( ) ;
2024-03-25 11:34:50 +08:00
const [ isOpen , setIsOpen ] = useState ( false ) ;
const { mutateAsync : disable2FA } = trpc . twoFactorAuthentication . disable . useMutation ( ) ;
2023-12-01 05:52:16 +05:30
2024-03-25 11:34:50 +08:00
const disable2FAForm = useForm < TDisable2FAForm > ( {
2023-12-01 05:52:16 +05:30
defaultValues : {
2024-03-25 11:34:50 +08:00
token : '' ,
2023-12-01 05:52:16 +05:30
} ,
2024-03-25 11:34:50 +08:00
resolver : zodResolver ( ZDisable2FAForm ) ,
2023-12-01 05:52:16 +05:30
} ) ;
2024-03-25 11:34:50 +08:00
const { isSubmitting : isDisable2FASubmitting } = disable2FAForm . formState ;
2023-12-01 05:52:16 +05:30
2024-03-25 11:34:50 +08:00
const onDisable2FAFormSubmit = async ( { token } : TDisable2FAForm ) = > {
2023-12-01 05:52:16 +05:30
try {
2024-03-25 11:34:50 +08:00
await disable2FA ( { token } ) ;
2023-12-01 05:52:16 +05:30
toast ( {
title : 'Two-factor authentication disabled' ,
description :
'Two-factor authentication has been disabled for your account. You will no longer be required to enter a code from your authenticator app when signing in.' ,
} ) ;
flushSync ( ( ) = > {
2024-03-25 11:34:50 +08:00
setIsOpen ( false ) ;
2023-12-01 05:52:16 +05:30
} ) ;
router . refresh ( ) ;
} catch ( _err ) {
toast ( {
title : 'Unable to disable two-factor authentication' ,
description :
'We were unable to disable two-factor authentication for your account. Please ensure that you have entered your password and backup code correctly and try again.' ,
variant : 'destructive' ,
} ) ;
}
} ;
return (
2024-03-25 11:34:50 +08:00
< Dialog open = { isOpen } onOpenChange = { setIsOpen } >
< DialogTrigger asChild = { true } >
< Button className = "flex-shrink-0" variant = "destructive" >
Disable 2 FA
< / Button >
< / DialogTrigger >
< DialogContent position = "center" >
2023-12-01 05:52:16 +05:30
< DialogHeader >
2024-03-25 11:34:50 +08:00
< DialogTitle > Disable 2 FA < / DialogTitle >
2023-12-01 05:52:16 +05:30
< DialogDescription >
2024-03-25 11:34:50 +08:00
Please provide a token from the authenticator , or a backup code . If you do not have a
backup code available , please contact support .
2023-12-01 05:52:16 +05:30
< / DialogDescription >
< / DialogHeader >
2024-03-25 11:34:50 +08:00
< Form { ...disable2FAForm } >
< form onSubmit = { disable2FAForm . handleSubmit ( onDisable2FAFormSubmit ) } >
< fieldset className = "flex flex-col gap-y-4" disabled = { isDisable2FASubmitting } >
2023-12-02 17:54:19 +05:30
< FormField
2024-03-25 11:34:50 +08:00
name = "token"
control = { disable2FAForm . control }
2023-12-02 17:54:19 +05:30
render = { ( { field } ) = > (
< FormItem >
< FormControl >
2024-03-25 11:34:50 +08:00
< Input { ...field } placeholder = "Token" / >
2023-12-02 17:54:19 +05:30
< / FormControl >
< FormMessage / >
< / FormItem >
) }
/ >
2024-03-25 11:34:50 +08:00
< DialogFooter >
< DialogClose asChild >
< Button type = "button" variant = "secondary" >
Cancel
< / Button >
< / DialogClose >
< Button type = "submit" variant = "destructive" loading = { isDisable2FASubmitting } >
Disable 2 FA
< / Button >
< / DialogFooter >
2023-12-02 17:54:19 +05:30
< / fieldset >
2023-12-01 05:52:16 +05:30
< / form >
< / Form >
< / DialogContent >
< / Dialog >
) ;
} ;