Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | "use client"; import { useState, useEffect } from "react"; import { Icon } from "@/components/ui/icons/Icon"; import Image from "next/image"; interface TwoFactorStatus { enabled: boolean; pendingSetup: boolean; backupCodesRemaining: number; } interface SetupData { qrCode: string; manualEntryCode: string; } type Step = "status" | "setup" | "verify" | "backup" | "disable" | "regenerate"; export function TwoFactorSetup() { const [status, setStatus] = useState<TwoFactorStatus | null>(null); const [setupData, setSetupData] = useState<SetupData | null>(null); const [backupCodes, setBackupCodes] = useState<string[]>([]); const [step, setStep] = useState<Step>("status"); const [verifyToken, setVerifyToken] = useState(""); const [disablePassword, setDisablePassword] = useState(""); const [regeneratePassword, setRegeneratePassword] = useState(""); const [loading, setLoading] = useState(true); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState<string | null>(null); const [showManualCode, setShowManualCode] = useState(false); useEffect(() => { fetchStatus(); }, []); async function fetchStatus() { setLoading(true); try { const response = await fetch("/api/user/two-factor"); if (!response.ok) throw new Error("Failed to fetch 2FA status"); const result = await response.json(); setStatus(result.data); } catch (err) { setError(err instanceof Error ? err.message : "An error occurred"); } finally { setLoading(false); } } async function handleSetup() { setSubmitting(true); setError(null); try { const response = await fetch("/api/user/two-factor?action=setup", { method: "POST", }); if (!response.ok) throw new Error("Failed to setup 2FA"); const result = await response.json(); setSetupData(result.data); setStep("setup"); } catch (err) { setError(err instanceof Error ? err.message : "Failed to setup 2FA"); } finally { setSubmitting(false); } } async function handleVerify() { if (verifyToken.length !== 6) { setError("Please enter a 6-digit code"); return; } setSubmitting(true); setError(null); try { const response = await fetch("/api/user/two-factor?action=enable", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token: verifyToken }), }); if (!response.ok) { const result = await response.json(); throw new Error(result.error || "Invalid verification code"); } const result = await response.json(); setBackupCodes(result.data.backupCodes || []); setStep("backup"); fetchStatus(); } catch (err) { setError(err instanceof Error ? err.message : "Verification failed"); } finally { setSubmitting(false); } } async function handleDisable() { if (!disablePassword) { setError("Please enter your password"); return; } setSubmitting(true); setError(null); try { const response = await fetch("/api/user/two-factor", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password: disablePassword }), }); if (!response.ok) throw new Error("Failed to disable 2FA"); setStep("status"); setDisablePassword(""); fetchStatus(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to disable 2FA"); } finally { setSubmitting(false); } } async function handleRegenerateCodes() { if (!regeneratePassword) { setError("Please enter your password"); return; } setSubmitting(true); setError(null); try { const response = await fetch( "/api/user/two-factor?action=regenerate-codes", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password: regeneratePassword }), } ); if (!response.ok) { const result = await response.json(); throw new Error(result.error || "Failed to regenerate codes"); } const result = await response.json(); setBackupCodes(result.data.backupCodes || []); setStep("backup"); setRegeneratePassword(""); fetchStatus(); } catch (err) { setError( err instanceof Error ? err.message : "Failed to regenerate codes" ); } finally { setSubmitting(false); } } if (loading) { return ( <div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6"> <div className="animate-pulse space-y-4"> <div className="h-6 bg-gray-200 dark:bg-gray-700 rounded w-1/3" /> <div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-2/3" /> </div> </div> ); } return ( <div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6"> {error && ( <div className="mb-4 p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg"> <p className="text-sm text-red-700 dark:text-red-400">{error}</p> </div> )} {/* Status View */} {step === "status" && status && ( <div className="space-y-4"> <div className="flex items-center justify-between"> <div className="flex items-center gap-3"> <div className={`p-2 rounded-full ${ status.enabled ? "bg-green-100 dark:bg-green-900/30 text-green-600 dark:text-green-400" : "bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400" }`} > <Icon name="shield-check" size={24} /> </div> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100"> Two-Factor Authentication </h3> <p className="text-sm text-gray-500 dark:text-gray-400"> {status.enabled ? "Your account is protected with 2FA" : "Add an extra layer of security to your account"} </p> </div> </div> <span className={`px-3 py-1 text-sm font-medium rounded-full ${ status.enabled ? "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400" : "bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-400" }`} > {status.enabled ? "Enabled" : "Disabled"} </span> </div> {status.enabled ? ( <div className="space-y-4 pt-4 border-t border-gray-200 dark:border-gray-700"> <div className="flex items-center justify-between p-3 bg-gray-50 dark:bg-gray-700/50 rounded-lg"> <div> <p className="text-sm font-medium text-gray-900 dark:text-gray-100"> Backup Codes Remaining </p> <p className="text-xs text-gray-500 dark:text-gray-400"> Use these if you lose access to your authenticator </p> </div> <div className="flex items-center gap-3"> <span className={`text-lg font-bold ${ status.backupCodesRemaining < 3 ? "text-red-600 dark:text-red-400" : "text-gray-900 dark:text-gray-100" }`} > {status.backupCodesRemaining} </span> <button onClick={() => setStep("regenerate")} disabled={submitting} className="text-sm text-blue-600 dark:text-blue-400 hover:underline" > Regenerate </button> </div> </div> <button onClick={() => setStep("disable")} className="w-full px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition-colors" > Disable Two-Factor Authentication </button> </div> ) : ( <button onClick={handleSetup} disabled={submitting} className="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-blue-400 text-white rounded-lg transition-colors flex items-center justify-center gap-2" > {submitting ? ( <> <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full" /> Setting up... </> ) : ( <> <Icon name="shield-check" size={18} /> Enable Two-Factor Authentication </> )} </button> )} </div> )} {/* Setup View */} {step === "setup" && setupData && ( <div className="space-y-6"> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2"> Scan QR Code </h3> <p className="text-sm text-gray-500 dark:text-gray-400 mb-4"> Use your authenticator app (Google Authenticator, Authy, etc.) to scan this QR code. </p> <div className="flex justify-center p-4 bg-white rounded-lg"> <Image src={setupData.qrCode} alt="2FA QR Code" width={200} height={200} /> </div> </div> <div> <button onClick={() => setShowManualCode(!showManualCode)} className="text-sm text-blue-600 dark:text-blue-400 hover:underline flex items-center gap-1" > <Icon name={showManualCode ? "chevron-up" : "chevron-down"} size={16} /> {showManualCode ? "Hide" : "Show"} manual entry code </button> {showManualCode && ( <div className="mt-2 p-3 bg-gray-50 dark:bg-gray-700/50 rounded-lg"> <p className="text-xs text-gray-500 dark:text-gray-400 mb-1"> Manual entry code: </p> <code className="text-sm font-mono text-gray-900 dark:text-gray-100"> {setupData.manualEntryCode} </code> </div> )} </div> <button onClick={() => setStep("verify")} className="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors" > Continue </button> </div> )} {/* Verify View */} {step === "verify" && ( <div className="space-y-6"> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2"> Verify Setup </h3> <p className="text-sm text-gray-500 dark:text-gray-400"> Enter the 6-digit code from your authenticator app to verify the setup. </p> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Verification Code </label> <input type="text" value={verifyToken} onChange={(e) => setVerifyToken(e.target.value.replace(/\D/g, "").slice(0, 6)) } placeholder="000000" maxLength={6} className="w-full px-4 py-3 text-center text-2xl font-mono tracking-widest bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-900 dark:text-gray-100" /> </div> <div className="flex gap-3"> <button onClick={() => setStep("setup")} className="flex-1 px-4 py-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-lg transition-colors" > Back </button> <button onClick={handleVerify} disabled={submitting || verifyToken.length !== 6} className="flex-1 px-4 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-blue-400 text-white rounded-lg transition-colors flex items-center justify-center gap-2" > {submitting ? ( <> <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full" /> Verifying... </> ) : ( "Verify & Enable" )} </button> </div> </div> )} {/* Backup Codes View */} {step === "backup" && backupCodes.length > 0 && ( <div className="space-y-6"> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2"> Save Your Backup Codes </h3> <p className="text-sm text-gray-500 dark:text-gray-400"> Store these codes in a safe place. Each code can only be used once. </p> </div> <div className="p-4 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg"> <div className="flex items-start gap-3"> <Icon name="alert-triangle" size={20} className="text-yellow-600 dark:text-yellow-400 flex-shrink-0 mt-0.5" /> <p className="text-sm text-yellow-700 dark:text-yellow-300"> These codes will only be shown once. Make sure to save them now! </p> </div> </div> <div className="grid grid-cols-2 gap-2 p-4 bg-gray-50 dark:bg-gray-700/50 rounded-lg"> {backupCodes.map((code, index) => ( <code key={index} className="px-3 py-2 bg-white dark:bg-gray-800 rounded text-center font-mono text-sm text-gray-900 dark:text-gray-100" > {code} </code> ))} </div> <div className="flex gap-3"> <button onClick={() => { const text = backupCodes.join("\n"); navigator.clipboard.writeText(text); }} className="flex-1 px-4 py-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-lg transition-colors flex items-center justify-center gap-2" > <Icon name="copy" size={18} /> Copy Codes </button> <button onClick={() => { setStep("status"); setBackupCodes([]); }} className="flex-1 px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors" > Done </button> </div> </div> )} {/* Disable View */} {step === "disable" && ( <div className="space-y-6"> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2"> Disable Two-Factor Authentication </h3> <p className="text-sm text-gray-500 dark:text-gray-400"> This will remove 2FA protection from your account. Enter your password to confirm. </p> </div> <div className="p-4 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg"> <div className="flex items-start gap-3"> <Icon name="alert-circle" size={20} className="text-red-600 dark:text-red-400 flex-shrink-0 mt-0.5" /> <p className="text-sm text-red-700 dark:text-red-300"> Warning: Disabling 2FA will make your account less secure. </p> </div> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Password </label> <input type="password" value={disablePassword} onChange={(e) => setDisablePassword(e.target.value)} placeholder="Enter your password" className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-900 dark:text-gray-100" /> </div> <div className="flex gap-3"> <button onClick={() => { setStep("status"); setDisablePassword(""); }} className="flex-1 px-4 py-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-lg transition-colors" > Cancel </button> <button onClick={handleDisable} disabled={submitting || !disablePassword} className="flex-1 px-4 py-2 bg-red-600 hover:bg-red-700 disabled:bg-red-400 text-white rounded-lg transition-colors flex items-center justify-center gap-2" > {submitting ? ( <> <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full" /> Disabling... </> ) : ( "Disable 2FA" )} </button> </div> </div> )} {/* Regenerate Codes View */} {step === "regenerate" && ( <div className="space-y-6"> <div> <h3 className="text-lg font-semibold text-gray-900 dark:text-gray-100 mb-2"> Regenerate Backup Codes </h3> <p className="text-sm text-gray-500 dark:text-gray-400"> This will generate new backup codes and invalidate your existing ones. Enter your password to confirm. </p> </div> <div className="p-4 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg"> <div className="flex items-start gap-3"> <Icon name="alert-triangle" size={20} className="text-yellow-600 dark:text-yellow-400 flex-shrink-0 mt-0.5" /> <p className="text-sm text-yellow-700 dark:text-yellow-300"> Warning: All existing backup codes will be invalidated. </p> </div> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"> Password </label> <input type="password" value={regeneratePassword} onChange={(e) => setRegeneratePassword(e.target.value)} placeholder="Enter your password" className="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-900 dark:text-gray-100" /> </div> <div className="flex gap-3"> <button onClick={() => { setStep("status"); setRegeneratePassword(""); }} className="flex-1 px-4 py-2 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-lg transition-colors" > Cancel </button> <button onClick={handleRegenerateCodes} disabled={submitting || !regeneratePassword} className="flex-1 px-4 py-2 bg-blue-600 hover:bg-blue-700 disabled:bg-blue-400 text-white rounded-lg transition-colors flex items-center justify-center gap-2" > {submitting ? ( <> <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full" /> Regenerating... </> ) : ( "Regenerate Codes" )} </button> </div> </div> )} </div> ); } export default TwoFactorSetup; |