|
1
|
import { |
|
2
|
Button, |
|
3
|
Copy, |
|
4
|
FocusModal, |
|
5
|
Heading, |
|
6
|
Hint, |
|
7
|
OtpInput, |
|
8
|
Text, |
|
9
|
} from "@medusajs/ui" |
|
10
|
import type { AuthMfaSetupResponse } from "@medusajs/js-sdk" |
|
11
|
import { useState } from "react" |
|
12
|
import { useTranslation } from "react-i18next" |
|
13
|
import { |
|
14
|
useGenerateAuthMfaRecoveryCodes, |
|
15
|
useVerifyAuthMfa, |
|
16
|
} from "../../../../../hooks/api" |
|
17
|
import { MfaQrCode } from "./mfa-qr-code" |
|
18
|
|
|
19
|
type MfaSetupModalProps = { |
|
20
|
setup: AuthMfaSetupResponse |
|
21
|
onClose: () => void |
|
22
|
} |
|
23
|
|
|
24
|
export const MfaSetupModal = ({ setup, onClose }: MfaSetupModalProps) => { |
|
25
|
const { t } = useTranslation() |
|
26
|
const [step, setStep] = useState<"verify" | "recovery-codes">("verify") |
|
27
|
const [code, setCode] = useState("") |
|
28
|
const [error, setError] = useState<string | null>(null) |
|
29
|
const [isVerified, setIsVerified] = useState(false) |
|
30
|
const [recoveryCodes, setRecoveryCodes] = useState<string[]>([]) |
|
31
|
const secret = setup.secret |
|
32
|
const otpauthUrl = setup.otpauth_url |
|
33
|
const { mutateAsync: verify, isPending: isVerifying } = useVerifyAuthMfa( |
|
34
|
setup.mfa_factor.id |
|
35
|
) |
|
36
|
const { mutateAsync: generateRecoveryCodes, isPending: isGenerating } = |
|
37
|
useGenerateAuthMfaRecoveryCodes() |
|
38
|
|
|
39
|
const generateAndShowRecoveryCodes = async () => { |
|
40
|
setError(null) |
|
41
|
|
|
42
|
try { |
|
43
|
const { recovery_codes } = await generateRecoveryCodes() |
|
44
|
setRecoveryCodes(recovery_codes) |
|
45
|
setStep("recovery-codes") |
|
46
|
} catch (e) { |
|
47
|
setError( |
|
48
|
e instanceof Error ? e.message : t("profile.mfa.recoveryCodesError") |
|
49
|
) |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
const handleVerify = async (nextCode = code) => { |
|
54
|
if (isVerified) { |
|
55
|
await generateAndShowRecoveryCodes() |
|
56
|
return |
|
57
|
} |
|
58
|
|
|
59
|
if (nextCode.length !== 6) { |
|
60
|
return |
|
61
|
} |
|
62
|
|
|
63
|
setError(null) |
|
64
|
|
|
65
|
try { |
|
66
|
await verify({ code: nextCode }) |
|
67
|
setIsVerified(true) |
|
68
|
} catch (e) { |
|
69
|
setError(e instanceof Error ? e.message : t("profile.mfa.verifyError")) |
|
70
|
setCode("") |
|
71
|
return |
|
72
|
} |
|
73
|
|
|
74
|
await generateAndShowRecoveryCodes() |
|
75
|
} |
|
76
|
|
|
77
|
const handleOtpChange = (value: string) => { |
|
78
|
setCode(value) |
|
79
|
} |
|
80
|
|
|
81
|
const handleDownloadRecoveryCodes = () => { |
|
82
|
const blob = new Blob([recoveryCodes.join("\n")], { |
|
83
|
type: "text/plain;charset=utf-8", |
|
84
|
}) |
|
85
|
const url = URL.createObjectURL(blob) |
|
86
|
const link = document.createElement("a") |
|
87
|
|
|
88
|
link.href = url |
|
89
|
link.download = "medusa-recovery-codes.txt" |
|
90
|
link.click() |
|
91
|
|
|
92
|
URL.revokeObjectURL(url) |
|
93
|
} |
|
94
|
|
|
95
|
return ( |
|
96
|
<FocusModal open onOpenChange={(open) => !open && onClose()}> |
|
97
|
<FocusModal.Content className="inset-auto left-1/2 top-1/2 max-h-[calc(100vh-32px)] w-[calc(100vw-32px)] max-w-[560px] -translate-x-1/2 -translate-y-1/2"> |
|
98
|
<FocusModal.Header> |
|
99
|
<FocusModal.Title asChild> |
|
100
|
<span className="sr-only"> |
|
101
|
{step === "verify" |
|
102
|
? t("profile.mfa.setupTitle") |
|
103
|
: t("profile.mfa.recoveryCodesTitle")} |
|
104
|
</span> |
|
105
|
</FocusModal.Title> |
|
106
|
</FocusModal.Header> |
|
107
|
<FocusModal.Body className="overflow-y-auto p-6"> |
|
108
|
{step === "verify" ? ( |
|
109
|
<div className="flex w-full flex-col items-center gap-y-6"> |
|
110
|
<div className="flex flex-col items-center gap-y-2 text-center"> |
|
111
|
<Heading>{t("profile.mfa.setupAuthenticatorApp")}</Heading> |
|
112
|
<Text size="small" className="text-ui-fg-subtle"> |
|
113
|
{t("profile.mfa.setupDescription")} |
|
114
|
</Text> |
|
115
|
</div> |
|
116
|
{secret && otpauthUrl ? ( |
|
117
|
<div className="border-ui-border-base flex w-full flex-col items-center gap-y-4 rounded-lg border p-4"> |
|
118
|
<div className="bg-ui-bg-subtle txt-compact-small text-ui-fg-base border-ui-border-base flex max-w-full items-center gap-x-2 rounded-md border px-3 py-2 font-mono"> |
|
119
|
<span className="truncate">{secret}</span> |
|
120
|
<Copy content={secret} variant="mini" /> |
|
121
|
</div> |
|
122
|
<MfaQrCode value={otpauthUrl} /> |
|
123
|
</div> |
|
124
|
) : ( |
|
125
|
<Hint className="inline-flex" variant="error"> |
|
126
|
{t("profile.mfa.setupError")} |
|
127
|
</Hint> |
|
128
|
)} |
|
129
|
<div className="flex flex-col items-center gap-y-3"> |
|
130
|
<OtpInput |
|
131
|
value={code} |
|
132
|
onChange={handleOtpChange} |
|
133
|
onComplete={handleVerify} |
|
134
|
disabled={ |
|
135
|
isVerifying || isGenerating || !secret || isVerified |
|
136
|
} |
|
137
|
autoFocus |
|
138
|
/> |
|
139
|
{error && ( |
|
140
|
<Hint className="inline-flex" variant="error"> |
|
141
|
{error} |
|
142
|
</Hint> |
|
143
|
)} |
|
144
|
</div> |
|
145
|
</div> |
|
146
|
) : ( |
|
147
|
<div className="flex w-full flex-col gap-y-4"> |
|
148
|
<div> |
|
149
|
<Heading>{t("profile.mfa.recoveryCodesTitle")}</Heading> |
|
150
|
<Text size="small" className="text-ui-fg-subtle"> |
|
151
|
{t("profile.mfa.recoveryCodesDescription")} |
|
152
|
</Text> |
|
153
|
</div> |
|
154
|
<div className="border-ui-border-base bg-ui-bg-subtle grid grid-cols-2 gap-3 rounded-lg border p-4"> |
|
155
|
{recoveryCodes.map((recoveryCode) => { |
|
156
|
return ( |
|
157
|
<Text key={recoveryCode} size="small" className="font-mono"> |
|
158
|
{recoveryCode} |
|
159
|
</Text> |
|
160
|
) |
|
161
|
})} |
|
162
|
</div> |
|
163
|
</div> |
|
164
|
)} |
|
165
|
</FocusModal.Body> |
|
166
|
<FocusModal.Footer> |
|
167
|
{step === "verify" ? ( |
|
168
|
<> |
|
169
|
<Button variant="secondary" onClick={onClose}> |
|
170
|
{t("actions.cancel")} |
|
171
|
</Button> |
|
172
|
<Button |
|
173
|
isLoading={isVerifying || isGenerating} |
|
174
|
disabled={(!isVerified && code.length !== 6) || !secret} |
|
175
|
onClick={() => handleVerify()} |
|
176
|
> |
|
177
|
{t("actions.confirm")} |
|
178
|
</Button> |
|
179
|
</> |
|
180
|
) : ( |
|
181
|
<> |
|
182
|
<Button variant="secondary" onClick={handleDownloadRecoveryCodes}> |
|
183
|
{t("actions.download")} |
|
184
|
</Button> |
|
185
|
<Copy content={recoveryCodes.join("\n")} asChild> |
|
186
|
<Button variant="secondary">{t("actions.copy")}</Button> |
|
187
|
</Copy> |
|
188
|
<Button onClick={onClose}>{t("actions.complete")}</Button> |
|
189
|
</> |
|
190
|
)} |
|
191
|
</FocusModal.Footer> |
|
192
|
</FocusModal.Content> |
|
193
|
</FocusModal> |
|
194
|
) |
|
195
|
} |