forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo-fa-setup-dialog.tsx
More file actions
287 lines (268 loc) 路 8.99 KB
/
Copy pathtwo-fa-setup-dialog.tsx
File metadata and controls
287 lines (268 loc) 路 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Loader2 } from 'lucide-react'
import { QRCodeSVG } from 'qrcode.react'
import { useState, useEffect, useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import { CopyButton } from '@/components/copy-button'
import { Dialog } from '@/components/dialog'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { setup2FA, enable2FA } from '@/lib/api'
import type { TwoFASetupData } from '../../types'
// ============================================================================
// Two-FA Setup Dialog Component
// ============================================================================
interface TwoFASetupDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
onSuccess: () => void
}
export function TwoFASetupDialog({
open,
onOpenChange,
onSuccess,
}: TwoFASetupDialogProps) {
const { t } = useTranslation()
const [loading, setLoading] = useState(false)
const [initializing, setInitializing] = useState(false)
const [step, setStep] = useState(0)
const [setupData, setSetupData] = useState<TwoFASetupData | null>(null)
const [code, setCode] = useState('')
const stepLabels = [
t('Scan QR Code'),
t('Save Backup Codes'),
t('Verify Setup'),
]
const handleSetup = useCallback(async () => {
try {
setInitializing(true)
const response = await setup2FA()
if (response.success && response.data) {
setSetupData(response.data)
setStep(0)
} else {
toast.error(response.message || t('Failed to setup 2FA'))
onOpenChange(false)
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Setup 2FA error:', error)
toast.error(t('Failed to setup 2FA'))
onOpenChange(false)
} finally {
setInitializing(false)
}
}, [onOpenChange, t])
const handleEnable = async () => {
if (!code) {
toast.error(t('Please enter the verification code'))
return
}
try {
setLoading(true)
const response = await enable2FA(code)
if (response.success) {
toast.success(t('Two-factor authentication enabled successfully!'))
onOpenChange(false)
onSuccess()
// Reset
setStep(0)
setCode('')
setSetupData(null)
} else {
toast.error(response.message || t('Failed to enable 2FA'))
}
} catch (_error) {
toast.error(t('Failed to enable 2FA'))
} finally {
setLoading(false)
}
}
const handleOpenChange = (open: boolean) => {
if (!loading && !initializing) {
if (open && !setupData) {
handleSetup()
}
if (!open) {
setStep(0)
setCode('')
setSetupData(null)
}
onOpenChange(open)
}
}
// Initialize when dialog opens
useEffect(() => {
if (open && !setupData && !initializing) {
handleSetup()
}
}, [open, setupData, initializing, handleSetup])
return (
<Dialog
open={open}
onOpenChange={handleOpenChange}
title={t('Setup Two-Factor Authentication')}
description={
<>
{t('Step')}
{step + 1}
{t('of 3:')}
{stepLabels[step]}
</>
}
contentClassName='sm:max-w-lg'
contentHeight='auto'
bodyClassName='space-y-4'
footer={
<>
{step > 0 && (
<Button
variant='outline'
onClick={() => setStep(step - 1)}
disabled={initializing || loading}
>
{t('Back')}
</Button>
)}
{step < 2 ? (
<Button
onClick={() => setStep(step + 1)}
disabled={initializing || !setupData}
>
{t('Next')}
</Button>
) : (
<Button
onClick={handleEnable}
disabled={initializing || loading || !code}
>
{loading && <Loader2 className='mr-2 h-4 w-4 animate-spin' />}
{loading ? t('Enabling...') : t('Enable 2FA')}
</Button>
)}
</>
}
>
<div className='space-y-4 py-4'>
{initializing ? (
<div className='flex flex-col items-center justify-center gap-3 py-8'>
<div className='border-primary h-8 w-8 animate-spin rounded-full border-4 border-t-transparent' />
<div className='text-muted-foreground text-sm'>
{t('Setting up 2FA...')}
</div>
</div>
) : !setupData ? (
<div className='flex justify-center py-8'>
<div className='text-muted-foreground'>
{t('Failed to load setup data')}
</div>
</div>
) : (
<>
{/* Step 0: QR Code */}
{step === 0 && (
<div className='space-y-4'>
<p className='text-muted-foreground text-sm'>
{t(
'Scan this QR code with your authenticator app (Google Authenticator, Microsoft Authenticator, etc.)'
)}
</p>
<div className='flex justify-center rounded-lg bg-white p-4'>
<QRCodeSVG value={setupData.qr_code_data} size={200} />
</div>
<div className='bg-muted rounded-lg p-3'>
<div className='flex items-center justify-between'>
<div>
<p className='text-muted-foreground text-xs'>
{t('Or enter this key manually:')}
</p>
<code className='font-mono text-sm'>
{setupData.secret}
</code>
</div>
<CopyButton
value={setupData.secret}
variant='ghost'
tooltip={t('Copy secret key')}
aria-label={t('Copy secret key')}
/>
</div>
</div>
</div>
)}
{/* Step 1: Backup Codes */}
{step === 1 && (
<div className='space-y-4'>
<Alert>
<AlertDescription>
{t(
'Save these backup codes in a safe place. Each code can only be used once.'
)}
</AlertDescription>
</Alert>
<div className='rounded-lg border p-4'>
<div className='grid grid-cols-2 gap-2'>
{setupData.backup_codes.map((code, index) => (
<div
key={index}
className='bg-muted rounded-md p-2 text-center font-mono text-sm'
>
{code}
</div>
))}
</div>
</div>
<CopyButton
value={setupData.backup_codes.join('\n')}
variant='outline'
size='default'
className='w-full'
iconClassName='mr-2 size-4'
tooltip={t('Copy all backup codes')}
aria-label={t('Copy all backup codes')}
>
{t('Copy All Codes')}
</CopyButton>
</div>
)}
{/* Step 2: Verify */}
{step === 2 && (
<div className='space-y-4'>
<div className='space-y-2'>
<Label htmlFor='code'>{t('Verification Code')}</Label>
<Input
id='code'
value={code}
onChange={(e) => setCode(e.target.value)}
placeholder={t('Enter 6-digit code')}
maxLength={6}
disabled={loading}
/>
<p className='text-muted-foreground text-xs'>
{t('Enter the 6-digit code from your authenticator app')}
</p>
</div>
</div>
)}
</>
)}
</div>
</Dialog>
)
}