AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

two-factor-challenge.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Form, Head, setLayoutProps } from '@inertiajs/react';
  2. import { REGEXP_ONLY_DIGITS } from 'input-otp';
  3. import { useMemo, useState } from 'react';
  4. import InputError from '@/components/input-error';
  5. import { Button } from '@/components/ui/button';
  6. import { Input } from '@/components/ui/input';
  7. import {
  8. InputOTP,
  9. InputOTPGroup,
  10. InputOTPSlot,
  11. } from '@/components/ui/input-otp';
  12. import { OTP_MAX_LENGTH } from '@/hooks/use-two-factor-auth';
  13. import { store } from '@/routes/two-factor/login';
  14. export default function TwoFactorChallenge() {
  15. const [showRecoveryInput, setShowRecoveryInput] = useState<boolean>(false);
  16. const [code, setCode] = useState<string>('');
  17. const authConfigContent = useMemo<{
  18. title: string;
  19. description: string;
  20. toggleText: string;
  21. }>(() => {
  22. if (showRecoveryInput) {
  23. return {
  24. title: 'Recovery code',
  25. description:
  26. 'Please confirm access to your account by entering one of your emergency recovery codes.',
  27. toggleText: 'login using an authentication code',
  28. };
  29. }
  30. return {
  31. title: 'Authentication code',
  32. description:
  33. 'Enter the authentication code provided by your authenticator application.',
  34. toggleText: 'login using a recovery code',
  35. };
  36. }, [showRecoveryInput]);
  37. setLayoutProps({
  38. title: authConfigContent.title,
  39. description: authConfigContent.description,
  40. });
  41. const toggleRecoveryMode = (clearErrors: () => void): void => {
  42. setShowRecoveryInput(!showRecoveryInput);
  43. clearErrors();
  44. setCode('');
  45. };
  46. return (
  47. <>
  48. <Head title="Two-factor authentication" />
  49. <div className="space-y-6">
  50. <Form
  51. {...store.form()}
  52. className="space-y-4"
  53. resetOnError
  54. resetOnSuccess={!showRecoveryInput}
  55. >
  56. {({ errors, processing, clearErrors }) => (
  57. <>
  58. {showRecoveryInput ? (
  59. <>
  60. <Input
  61. name="recovery_code"
  62. type="text"
  63. placeholder="Enter recovery code"
  64. autoFocus={showRecoveryInput}
  65. required
  66. />
  67. <InputError
  68. message={errors.recovery_code}
  69. />
  70. </>
  71. ) : (
  72. <div className="flex flex-col items-center justify-center space-y-3 text-center">
  73. <div className="flex w-full items-center justify-center">
  74. <InputOTP
  75. name="code"
  76. maxLength={OTP_MAX_LENGTH}
  77. value={code}
  78. onChange={(value) => setCode(value)}
  79. disabled={processing}
  80. pattern={REGEXP_ONLY_DIGITS}
  81. autoFocus
  82. >
  83. <InputOTPGroup>
  84. {Array.from(
  85. { length: OTP_MAX_LENGTH },
  86. (_, index) => (
  87. <InputOTPSlot
  88. key={index}
  89. index={index}
  90. />
  91. ),
  92. )}
  93. </InputOTPGroup>
  94. </InputOTP>
  95. </div>
  96. <InputError message={errors.code} />
  97. </div>
  98. )}
  99. <Button
  100. type="submit"
  101. className="w-full"
  102. disabled={processing}
  103. >
  104. Continue
  105. </Button>
  106. <div className="text-center text-sm text-muted-foreground">
  107. <span>or you can </span>
  108. <button
  109. type="button"
  110. className="cursor-pointer text-foreground underline decoration-neutral-300 underline-offset-4 transition-colors duration-300 ease-out hover:decoration-current! dark:decoration-neutral-500"
  111. onClick={() =>
  112. toggleRecoveryMode(clearErrors)
  113. }
  114. >
  115. {authConfigContent.toggleText}
  116. </button>
  117. </div>
  118. </>
  119. )}
  120. </Form>
  121. </div>
  122. </>
  123. );
  124. }