|
| 1 | +import React from "react"; |
| 2 | +import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form"; |
| 3 | + |
| 4 | +import AuthChallengeWidget from "./providers/AuthChallengeWidget"; |
| 5 | +import FriendlyCaptchaWidget from "./providers/FriendlyCaptchaWidget"; |
| 6 | +import HCaptchaWidget from "./providers/HCaptchaWidget"; |
| 7 | +import RecaptchaCombinedWidget from "./providers/ReCaptchaCombinedWidget"; |
| 8 | +import SimpleCaptchaWidget from "./providers/SimpleCaptchaWidget"; |
| 9 | + |
| 10 | +// --- |
| 11 | +// Interfaces |
| 12 | +// --- |
| 13 | + |
| 14 | +export interface CaptchaResponse { |
| 15 | + provider: string; |
| 16 | + token?: string; |
| 17 | + answer?: string; |
| 18 | + arkoseToken?: string; |
| 19 | +} |
| 20 | + |
| 21 | +export interface CaptchaWidgetProps<T extends FieldValues = FieldValues> { |
| 22 | + config: { |
| 23 | + provider: string; |
| 24 | + siteKey?: string; |
| 25 | + image?: string; |
| 26 | + size?: string; |
| 27 | + placeholder?: string; |
| 28 | + }; |
| 29 | + control?: Control<T>; |
| 30 | + rules?: RegisterOptions<T>; |
| 31 | + name: Path<T>; |
| 32 | + onCaptchaResponse: (response: CaptchaResponse | null) => void; |
| 33 | + className?: string; |
| 34 | + label?: string; |
| 35 | + theme?: "light" | "dark" | "auto"; |
| 36 | + error?: string; |
| 37 | +} |
| 38 | + |
| 39 | +export interface ICaptcha { |
| 40 | + provider?: string; |
| 41 | + image?: string; |
| 42 | + imageAltText?: string; |
| 43 | + enabled?: boolean; |
| 44 | + siteKey?: string; |
| 45 | +} |
| 46 | + |
| 47 | +export interface CaptchaProps<T extends FieldValues = FieldValues> { |
| 48 | + captcha?: ICaptcha; |
| 49 | + onValidationChange?: ( |
| 50 | + isValid: boolean, |
| 51 | + value?: string, |
| 52 | + error?: string |
| 53 | + ) => void; |
| 54 | + label?: string; |
| 55 | + sdkError?: string; |
| 56 | + theme?: "light" | "dark" | "auto"; |
| 57 | + className?: string; |
| 58 | + control: Control<T>; |
| 59 | + rules?: RegisterOptions<T>; |
| 60 | + name: Path<T>; |
| 61 | +} |
| 62 | + |
| 63 | +// --- |
| 64 | +// Main Component |
| 65 | +// --- |
| 66 | + |
| 67 | +const Captcha = <T extends FieldValues = FieldValues>({ |
| 68 | + control, |
| 69 | + rules, |
| 70 | + name, |
| 71 | + captcha, |
| 72 | + onValidationChange, |
| 73 | + label, |
| 74 | + sdkError, |
| 75 | + theme, |
| 76 | + className, |
| 77 | +}: CaptchaProps<T>) => { |
| 78 | + // --- |
| 79 | + // Constants and Mappings |
| 80 | + // --- |
| 81 | + |
| 82 | + function getCaptchaWidgetMap<T extends FieldValues>() { |
| 83 | + return { |
| 84 | + recaptcha_v2: RecaptchaCombinedWidget as React.ComponentType< |
| 85 | + CaptchaWidgetProps<T> |
| 86 | + >, |
| 87 | + recaptcha_enterprise: RecaptchaCombinedWidget as React.ComponentType< |
| 88 | + CaptchaWidgetProps<T> |
| 89 | + >, |
| 90 | + hcaptcha: HCaptchaWidget as React.ComponentType<CaptchaWidgetProps<T>>, |
| 91 | + auth0_v2: AuthChallengeWidget as React.ComponentType< |
| 92 | + CaptchaWidgetProps<T> |
| 93 | + >, |
| 94 | + friendly_captcha: FriendlyCaptchaWidget as React.ComponentType< |
| 95 | + CaptchaWidgetProps<T> |
| 96 | + >, |
| 97 | + }; |
| 98 | + } |
| 99 | + const CAPTCHA_WIDGET_MAP = getCaptchaWidgetMap<T>(); |
| 100 | + const { provider, image, siteKey, enabled = true } = captcha || {}; // Default 'enabled' to true |
| 101 | + |
| 102 | + // If captcha is not enabled or no provider is specified, render nothing. |
| 103 | + if (!enabled || !provider) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + const handleResponse = (res: CaptchaResponse | null) => { |
| 108 | + if (onValidationChange) { |
| 109 | + if (res) { |
| 110 | + const value = |
| 111 | + res.provider === "auth0" ? res.answer : res.token || res.arkoseToken; |
| 112 | + const isValid = !!value; |
| 113 | + onValidationChange(isValid, value); |
| 114 | + } else { |
| 115 | + onValidationChange(false); |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + // Handle the special case for SimpleCaptchaWidget (Auth0 v1) |
| 121 | + if (provider === "auth0") { |
| 122 | + return image ? ( |
| 123 | + <SimpleCaptchaWidget |
| 124 | + config={{ provider: "auth0", image }} |
| 125 | + onCaptchaResponse={handleResponse} |
| 126 | + control={control} |
| 127 | + name={name} |
| 128 | + rules={rules} |
| 129 | + label={label} |
| 130 | + error={sdkError} |
| 131 | + className={className} |
| 132 | + /> |
| 133 | + ) : null; |
| 134 | + } |
| 135 | + |
| 136 | + // Use the map for other providers |
| 137 | + const SpecificCaptchaWidget = |
| 138 | + CAPTCHA_WIDGET_MAP[provider as keyof typeof CAPTCHA_WIDGET_MAP]; |
| 139 | + |
| 140 | + if (SpecificCaptchaWidget && siteKey) { |
| 141 | + return ( |
| 142 | + <SpecificCaptchaWidget |
| 143 | + config={{ provider, siteKey }} |
| 144 | + name={name} |
| 145 | + onCaptchaResponse={handleResponse} |
| 146 | + theme={theme} |
| 147 | + label={label} |
| 148 | + error={sdkError} |
| 149 | + className={className} |
| 150 | + /> |
| 151 | + ); |
| 152 | + } |
| 153 | + return null; |
| 154 | +}; |
| 155 | + |
| 156 | +export default Captcha; |
0 commit comments