1 import { Key, ShieldCheck } from "@medusajs/icons"
2 import type { AuthMfaSetupResponse } from "@medusajs/js-sdk"
3 import {
4 Badge,
5 Button,
6 Container,
7 Heading,
8 Text,
9 toast,
10 usePrompt,
11 } from "@medusajs/ui"
12 import type { AuthTypes } from "@medusajs/types"
13 import { useMemo, useState } from "react"
14 import { useTranslation } from "react-i18next"
15 import {
16 useAuthMfa,
17 useDisableAuthMfa,
18 useStartAuthMfa,
19 } from "../../../../../hooks/api"
20 import { MfaDisableModal } from "./mfa-disable-modal"
21 import { MfaSetupModal } from "./mfa-setup-modal"
22
23 const MFA_DISABLE_CODE_REQUIRED_ERROR =
24 "MFA verification code is required to disable MFA"
25
26 export const ProfileMfaSection = () => {
27 const { t } = useTranslation()
28 const prompt = usePrompt()
29 const { mfa_factors: factors = [], isPending } = useAuthMfa()
30 const [setupResponse, setSetupResponse] =
31 useState<AuthMfaSetupResponse | null>(null)
32 const [disableChallengeFactor, setDisableChallengeFactor] =
33 useState<AuthTypes.AuthMfaDTO | null>(null)
34 const { mutateAsync: startMfa, isPending: isStarting } = useStartAuthMfa()
35
36 const enabledFactor = useMemo(() => {
37 return factors.find((factor) => factor.status === "enabled")
38 }, [factors])
39
40 const pendingFactor = useMemo(() => {
41 return factors.find((factor) => factor.status === "pending")
42 }, [factors])
43
44 const { mutateAsync: disableMfa, isPending: isDisabling } = useDisableAuthMfa(
45 enabledFactor?.id ?? ""
46 )
47 const { mutateAsync: cancelPendingMfa, isPending: isCancellingPending } =
48 useDisableAuthMfa(pendingFactor?.id ?? "")
49
50 const handleSetup = async () => {
51 try {
52 if (pendingFactor) {
53 await cancelPendingMfa()
54 }
55
56 const response = await startMfa({
57 provider: "totp",
58 label: t("profile.mfa.authenticatorApp"),
59 })
60
61 setSetupResponse(response)
62 } catch (e) {
63 toast.error(e instanceof Error ? e.message : t("profile.mfa.setupError"))
64 }
65 }
66
67 const handleDisable = async () => {
68 if (!enabledFactor) {
69 return
70 }
71
72 const confirmed = await prompt({
73 title: t("profile.mfa.disableTitle"),
74 description: t("profile.mfa.disableDescription"),
75 confirmText: t("actions.disable"),
76 cancelText: t("actions.cancel"),
77 variant: "danger",
78 })
79
80 if (!confirmed) {
81 return
82 }
83
84 try {
85 await disableMfa()
86 toast.success(t("profile.mfa.disableSuccess"))
87 } catch (e) {
88 if (
89 e instanceof Error &&
90 e.message.includes(MFA_DISABLE_CODE_REQUIRED_ERROR)
91 ) {
92 setDisableChallengeFactor(enabledFactor)
93 return
94 }
95
96 toast.error(
97 e instanceof Error ? e.message : t("profile.mfa.disableError")
98 )
99 }
100 }
101
102 return (
103 <>
104 <Container className="divide-y p-0">
105 <div className="flex items-center justify-between px-6 py-4">
106 <div>
107 <Heading>{t("profile.mfa.title")}</Heading>
108 <Text className="text-ui-fg-subtle" size="small">
109 {t("profile.mfa.description")}
110 </Text>
111 </div>
112 {enabledFactor ? (
113 <Button
114 size="small"
115 variant="danger"
116 isLoading={isDisabling}
117 onClick={handleDisable}
118 >
119 {t("actions.disable")}
120 </Button>
121 ) : (
122 <Button
123 size="small"
124 variant="secondary"
125 isLoading={isStarting || isCancellingPending}
126 disabled={isPending}
127 onClick={handleSetup}
128 >
129 {t("actions.enable")}
130 </Button>
131 )}
132 </div>
133 <div className="grid grid-cols-2 items-center px-6 py-4">
134 <Text size="small" leading="compact" weight="plus">
135 {t("profile.mfa.status")}
136 </Text>
137 <div>
138 {enabledFactor ? (
139 <Badge color="green" size="2xsmall">
140 {t("profile.mfa.enabled")}
141 </Badge>
142 ) : pendingFactor ? (
143 <Badge color="orange" size="2xsmall">
144 {t("profile.mfa.pending")}
145 </Badge>
146 ) : (
147 <Badge color="grey" size="2xsmall">
148 {t("profile.mfa.disabled")}
149 </Badge>
150 )}
151 </div>
152 </div>
153 <div className="grid grid-cols-2 items-center px-6 py-4">
154 <Text size="small" leading="compact" weight="plus">
155 {t("profile.mfa.method")}
156 </Text>
157 <div className="flex items-center gap-x-2">
158 {enabledFactor || pendingFactor ? (
159 <>
160 <ShieldCheck className="text-ui-fg-subtle" />
161 <Text size="small" leading="compact">
162 {t("profile.mfa.authenticatorApp")}
163 </Text>
164 </>
165 ) : (
166 <>
167 <Key className="text-ui-fg-muted" />
168 <Text
169 size="small"
170 leading="compact"
171 className="text-ui-fg-subtle"
172 >
173 {t("profile.mfa.noMethod")}
174 </Text>
175 </>
176 )}
177 </div>
178 </div>
179 </Container>
180 {setupResponse && (
181 <MfaSetupModal
182 setup={setupResponse}
183 onClose={() => setSetupResponse(null)}
184 />
185 )}
186 {disableChallengeFactor && (
187 <MfaDisableModal
188 factor={disableChallengeFactor}
189 onClose={() => setDisableChallengeFactor(null)}
190 onSuccess={() => {
191 setDisableChallengeFactor(null)
192 toast.success(t("profile.mfa.disableSuccess"))
193 }}
194 />
195 )}
196 </>
197 )
198 }