1 import { Button, FocusModal, Heading, Hint, OtpInput, Text } from "@medusajs/ui"
2 import type { AuthTypes } from "@medusajs/types"
3 import { useState } from "react"
4 import { useTranslation } from "react-i18next"
5 import { useDisableAuthMfa } from "../../../../../hooks/api"
6
7 type MfaDisableModalProps = {
8 factor: AuthTypes.AuthMfaDTO
9 onClose: () => void
10 onSuccess: () => void
11 }
12
13 export const MfaDisableModal = ({
14 factor,
15 onClose,
16 onSuccess,
17 }: MfaDisableModalProps) => {
18 const { t } = useTranslation()
19 const [code, setCode] = useState("")
20 const [error, setError] = useState<string | null>(null)
21 const { mutateAsync, isPending } = useDisableAuthMfa(factor.id)
22
23 const handleDisable = async (nextCode = code) => {
24 if (nextCode.length !== 6) {
25 return
26 }
27
28 setError(null)
29
30 try {
31 await mutateAsync({
32 method: factor.provider,
33 code: nextCode,
34 })
35 onSuccess()
36 } catch (e) {
37 setError(e instanceof Error ? e.message : t("profile.mfa.disableError"))
38 setCode("")
39 }
40 }
41
42 const handleOtpChange = (value: string) => {
43 setCode(value)
44 }
45
46 return (
47 <FocusModal open onOpenChange={(open) => !open && onClose()}>
48 <FocusModal.Content>
49 <FocusModal.Header>
50 <FocusModal.Title>{t("profile.mfa.disableTitle")}</FocusModal.Title>
51 </FocusModal.Header>
52 <FocusModal.Body className="flex flex-1 items-center justify-center">
53 <div className="flex w-full max-w-[360px] flex-col items-center gap-y-6 text-center">
54 <div>
55 <Heading>{t("profile.mfa.disableTitle")}</Heading>
56 <Text size="small" className="text-ui-fg-subtle">
57 {t("profile.mfa.disableChallengeDescription")}
58 </Text>
59 </div>
60 <OtpInput
61 value={code}
62 onChange={handleOtpChange}
63 onComplete={handleDisable}
64 disabled={isPending}
65 autoFocus
66 />
67 {error && (
68 <Hint className="inline-flex" variant="error">
69 {error}
70 </Hint>
71 )}
72 </div>
73 </FocusModal.Body>
74 <FocusModal.Footer>
75 <Button variant="secondary" onClick={onClose}>
76 {t("actions.cancel")}
77 </Button>
78 <Button
79 variant="danger"
80 isLoading={isPending}
81 disabled={code.length !== 6}
82 onClick={() => handleDisable()}
83 >
84 {t("actions.disable")}
85 </Button>
86 </FocusModal.Footer>
87 </FocusModal.Content>
88 </FocusModal>
89 )
90 }