AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

two-factor-recovery-codes.tsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Form } from '@inertiajs/react';
  2. import { Eye, EyeOff, LockKeyhole, RefreshCw } from 'lucide-react';
  3. import { useCallback, useEffect, useRef, useState } from 'react';
  4. import AlertError from '@/components/alert-error';
  5. import { Button } from '@/components/ui/button';
  6. import {
  7. Card,
  8. CardContent,
  9. CardDescription,
  10. CardHeader,
  11. CardTitle,
  12. } from '@/components/ui/card';
  13. import { regenerateRecoveryCodes } from '@/routes/two-factor';
  14. type Props = {
  15. recoveryCodesList: string[];
  16. fetchRecoveryCodes: () => Promise<void>;
  17. errors: string[];
  18. };
  19. export default function TwoFactorRecoveryCodes({
  20. recoveryCodesList,
  21. fetchRecoveryCodes,
  22. errors,
  23. }: Props) {
  24. const [codesAreVisible, setCodesAreVisible] = useState<boolean>(false);
  25. const codesSectionRef = useRef<HTMLDivElement | null>(null);
  26. const canRegenerateCodes = recoveryCodesList.length > 0 && codesAreVisible;
  27. const toggleCodesVisibility = useCallback(async () => {
  28. if (!codesAreVisible && !recoveryCodesList.length) {
  29. await fetchRecoveryCodes();
  30. }
  31. setCodesAreVisible(!codesAreVisible);
  32. if (!codesAreVisible) {
  33. setTimeout(() => {
  34. codesSectionRef.current?.scrollIntoView({
  35. behavior: 'smooth',
  36. block: 'nearest',
  37. });
  38. });
  39. }
  40. }, [codesAreVisible, recoveryCodesList.length, fetchRecoveryCodes]);
  41. useEffect(() => {
  42. if (!recoveryCodesList.length) {
  43. void fetchRecoveryCodes();
  44. }
  45. }, [recoveryCodesList.length, fetchRecoveryCodes]);
  46. const RecoveryCodeIconComponent = codesAreVisible ? EyeOff : Eye;
  47. return (
  48. <Card>
  49. <CardHeader>
  50. <CardTitle className="flex gap-3">
  51. <LockKeyhole className="size-4" aria-hidden="true" />
  52. 2FA recovery codes
  53. </CardTitle>
  54. <CardDescription>
  55. Recovery codes let you regain access if you lose your 2FA
  56. device. Store them in a secure password manager.
  57. </CardDescription>
  58. </CardHeader>
  59. <CardContent>
  60. <div className="flex flex-col gap-3 select-none sm:flex-row sm:items-center sm:justify-between">
  61. <Button
  62. onClick={toggleCodesVisibility}
  63. className="w-fit"
  64. aria-expanded={codesAreVisible}
  65. aria-controls="recovery-codes-section"
  66. >
  67. <RecoveryCodeIconComponent
  68. className="size-4"
  69. aria-hidden="true"
  70. />
  71. {codesAreVisible ? 'Hide' : 'View'} recovery codes
  72. </Button>
  73. {canRegenerateCodes && (
  74. <Form
  75. {...regenerateRecoveryCodes.form()}
  76. options={{ preserveScroll: true }}
  77. onSuccess={fetchRecoveryCodes}
  78. >
  79. {({ processing }) => (
  80. <Button
  81. variant="secondary"
  82. type="submit"
  83. disabled={processing}
  84. aria-describedby="regenerate-warning"
  85. >
  86. <RefreshCw /> Regenerate codes
  87. </Button>
  88. )}
  89. </Form>
  90. )}
  91. </div>
  92. <div
  93. id="recovery-codes-section"
  94. className={`relative overflow-hidden transition-all duration-300 ${codesAreVisible ? 'h-auto opacity-100' : 'h-0 opacity-0'}`}
  95. aria-hidden={!codesAreVisible}
  96. >
  97. <div className="mt-3 space-y-3">
  98. {errors?.length ? (
  99. <AlertError errors={errors} />
  100. ) : (
  101. <>
  102. <div
  103. ref={codesSectionRef}
  104. className="grid gap-1 rounded-lg bg-muted p-4 font-mono text-sm"
  105. role="list"
  106. aria-label="Recovery codes"
  107. >
  108. {recoveryCodesList.length ? (
  109. recoveryCodesList.map((code, index) => (
  110. <div
  111. key={index}
  112. role="listitem"
  113. className="select-text"
  114. >
  115. {code}
  116. </div>
  117. ))
  118. ) : (
  119. <div
  120. className="space-y-2"
  121. aria-label="Loading recovery codes"
  122. >
  123. {Array.from(
  124. { length: 8 },
  125. (_, index) => (
  126. <div
  127. key={index}
  128. className="h-4 animate-pulse rounded bg-muted-foreground/20"
  129. aria-hidden="true"
  130. />
  131. ),
  132. )}
  133. </div>
  134. )}
  135. </div>
  136. <div className="text-xs text-muted-foreground select-none">
  137. <p id="regenerate-warning">
  138. Each recovery code can be used once to
  139. access your account and will be removed
  140. after use. If you need more, click{' '}
  141. <span className="font-bold">
  142. Regenerate codes
  143. </span>{' '}
  144. above.
  145. </p>
  146. </div>
  147. </>
  148. )}
  149. </div>
  150. </div>
  151. </CardContent>
  152. </Card>
  153. );
  154. }