AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

use-two-factor-auth.ts 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { useHttp } from '@inertiajs/react';
  2. import { useCallback, useState } from 'react';
  3. import { qrCode, recoveryCodes, secretKey } from '@/routes/two-factor';
  4. export type UseTwoFactorAuthReturn = {
  5. qrCodeSvg: string | null;
  6. manualSetupKey: string | null;
  7. recoveryCodesList: string[];
  8. hasSetupData: boolean;
  9. errors: string[];
  10. clearErrors: () => void;
  11. clearSetupData: () => void;
  12. clearTwoFactorAuthData: () => void;
  13. fetchQrCode: () => Promise<void>;
  14. fetchSetupKey: () => Promise<void>;
  15. fetchSetupData: () => Promise<void>;
  16. fetchRecoveryCodes: () => Promise<void>;
  17. };
  18. export const OTP_MAX_LENGTH = 6;
  19. export const useTwoFactorAuth = (): UseTwoFactorAuthReturn => {
  20. const { submit } = useHttp();
  21. const [qrCodeSvg, setQrCodeSvg] = useState<string | null>(null);
  22. const [manualSetupKey, setManualSetupKey] = useState<string | null>(null);
  23. const [recoveryCodesList, setRecoveryCodesList] = useState<string[]>([]);
  24. const [errors, setErrors] = useState<string[]>([]);
  25. const hasSetupData = qrCodeSvg !== null && manualSetupKey !== null;
  26. const clearErrors = useCallback((): void => {
  27. setErrors([]);
  28. }, []);
  29. const clearSetupData = useCallback((): void => {
  30. setManualSetupKey(null);
  31. setQrCodeSvg(null);
  32. setErrors([]);
  33. }, []);
  34. const clearTwoFactorAuthData = useCallback((): void => {
  35. setManualSetupKey(null);
  36. setQrCodeSvg(null);
  37. setErrors([]);
  38. setRecoveryCodesList([]);
  39. }, []);
  40. const fetchQrCode = useCallback(async (): Promise<void> => {
  41. try {
  42. const { svg } = (await submit(qrCode())) as {
  43. svg: string;
  44. url: string;
  45. };
  46. setQrCodeSvg(svg);
  47. } catch {
  48. setErrors((prev) => [...prev, 'Failed to fetch QR code']);
  49. setQrCodeSvg(null);
  50. }
  51. }, [submit]);
  52. const fetchSetupKey = useCallback(async (): Promise<void> => {
  53. try {
  54. const { secretKey: key } = (await submit(secretKey())) as {
  55. secretKey: string;
  56. };
  57. setManualSetupKey(key);
  58. } catch {
  59. setErrors((prev) => [...prev, 'Failed to fetch a setup key']);
  60. setManualSetupKey(null);
  61. }
  62. }, [submit]);
  63. const fetchRecoveryCodes = useCallback(async (): Promise<void> => {
  64. try {
  65. setErrors([]);
  66. const codes = (await submit(recoveryCodes())) as string[];
  67. setRecoveryCodesList(codes);
  68. } catch {
  69. setErrors((prev) => [...prev, 'Failed to fetch recovery codes']);
  70. setRecoveryCodesList([]);
  71. }
  72. }, [submit]);
  73. const fetchSetupData = useCallback(async (): Promise<void> => {
  74. try {
  75. setErrors([]);
  76. await Promise.all([fetchQrCode(), fetchSetupKey()]);
  77. } catch {
  78. setQrCodeSvg(null);
  79. setManualSetupKey(null);
  80. }
  81. }, [fetchQrCode, fetchSetupKey]);
  82. return {
  83. qrCodeSvg,
  84. manualSetupKey,
  85. recoveryCodesList,
  86. hasSetupData,
  87. errors,
  88. clearErrors,
  89. clearSetupData,
  90. clearTwoFactorAuthData,
  91. fetchQrCode,
  92. fetchSetupKey,
  93. fetchSetupData,
  94. fetchRecoveryCodes,
  95. };
  96. };