AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

manage-two-factor.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Form } from '@inertiajs/react';
  2. import { ShieldCheck } from 'lucide-react';
  3. import { useEffect, useRef, useState } from 'react';
  4. import Heading from '@/components/heading';
  5. import TwoFactorRecoveryCodes from '@/components/two-factor-recovery-codes';
  6. import TwoFactorSetupModal from '@/components/two-factor-setup-modal';
  7. import { Button } from '@/components/ui/button';
  8. import { useTwoFactorAuth } from '@/hooks/use-two-factor-auth';
  9. import { disable, enable } from '@/routes/two-factor';
  10. export type Props = {
  11. canManageTwoFactor?: boolean;
  12. requiresConfirmation?: boolean;
  13. twoFactorEnabled?: boolean;
  14. };
  15. export default function ManageTwoFactor(props: Props) {
  16. const requiresConfirmation = props.requiresConfirmation ?? false;
  17. const twoFactorEnabled = props.twoFactorEnabled ?? false;
  18. const {
  19. qrCodeSvg,
  20. hasSetupData,
  21. manualSetupKey,
  22. clearSetupData,
  23. clearTwoFactorAuthData,
  24. fetchSetupData,
  25. recoveryCodesList,
  26. fetchRecoveryCodes,
  27. errors,
  28. } = useTwoFactorAuth();
  29. const [showSetupModal, setShowSetupModal] = useState<boolean>(false);
  30. const prevTwoFactorEnabled = useRef(twoFactorEnabled);
  31. useEffect(() => {
  32. if (prevTwoFactorEnabled.current && !twoFactorEnabled) {
  33. clearTwoFactorAuthData();
  34. }
  35. prevTwoFactorEnabled.current = twoFactorEnabled;
  36. }, [twoFactorEnabled, clearTwoFactorAuthData]);
  37. if (!(props.canManageTwoFactor ?? false)) {
  38. return null;
  39. }
  40. return (
  41. <div className="space-y-6">
  42. <Heading
  43. variant="small"
  44. title="Two-factor authentication"
  45. description="Manage your two-factor authentication settings"
  46. />
  47. {twoFactorEnabled ? (
  48. <div className="flex flex-col items-start justify-start space-y-4">
  49. <p className="text-sm text-muted-foreground">
  50. You will be prompted for a secure, random pin during
  51. login, which you can retrieve from the TOTP-supported
  52. application on your phone.
  53. </p>
  54. <div className="relative inline">
  55. <Form {...disable.form()}>
  56. {({ processing }) => (
  57. <Button
  58. variant="destructive"
  59. type="submit"
  60. disabled={processing}
  61. >
  62. Disable 2FA
  63. </Button>
  64. )}
  65. </Form>
  66. </div>
  67. <TwoFactorRecoveryCodes
  68. recoveryCodesList={recoveryCodesList}
  69. fetchRecoveryCodes={fetchRecoveryCodes}
  70. errors={errors}
  71. />
  72. </div>
  73. ) : (
  74. <div className="flex flex-col items-start justify-start space-y-4">
  75. <p className="text-sm text-muted-foreground">
  76. When you enable two-factor authentication, you will be
  77. prompted for a secure pin during login. This pin can be
  78. retrieved from a TOTP-supported application on your
  79. phone.
  80. </p>
  81. <div>
  82. {hasSetupData ? (
  83. <Button onClick={() => setShowSetupModal(true)}>
  84. <ShieldCheck />
  85. Continue setup
  86. </Button>
  87. ) : (
  88. <Form
  89. {...enable.form()}
  90. onSuccess={() => setShowSetupModal(true)}
  91. >
  92. {({ processing }) => (
  93. <Button type="submit" disabled={processing}>
  94. Enable 2FA
  95. </Button>
  96. )}
  97. </Form>
  98. )}
  99. </div>
  100. </div>
  101. )}
  102. <TwoFactorSetupModal
  103. isOpen={showSetupModal}
  104. onClose={() => setShowSetupModal(false)}
  105. requiresConfirmation={requiresConfirmation}
  106. twoFactorEnabled={twoFactorEnabled}
  107. qrCodeSvg={qrCodeSvg}
  108. manualSetupKey={manualSetupKey}
  109. clearSetupData={clearSetupData}
  110. fetchSetupData={fetchSetupData}
  111. errors={errors}
  112. />
  113. </div>
  114. );
  115. }