react-native-recaptcha-that-works

repository·master·Indexed 41 days ago

https://github.com/douglasjunior/react-native-recaptcha-that-works

A reCAPTCHA bridge for React Native (Android and iOS) version 2.0.0 that provides support for normal, invisible, and Enterprise reCAPTCHA v2 via a WebView. It includes a Recaptcha component with methods to programmatically open and close the challenge modal.

Tokens
2.9K
Snippets
5
Records
9
Agent score
49%

What's inside react-native-recaptcha-that-works

  1. Install react-native-recaptcha-that-works

    master

    To use this library, you must install both react-native-recaptcha-that-works and react-native-webview.

    Using yarn:

    yarn add react-native-recaptcha-that-works react-native-webview

    Using npm:

    npm i -S react-native-recaptcha-that-works react-native-webview

    Note: You should also refer to the react-native-webview Getting Started Guide for platform-specific setup requirements.

    yarn add react-native-recaptcha-that-works react-native-webview
  2. Use Recaptcha in React Native (JavaScript and TypeScript)

    master

    The Recaptcha component is used to trigger the reCAPTCHA flow. You should use a useRef hook to access the component's methods, specifically .open() to trigger the challenge.

    JavaScript Example

    import React, { useRef } from 'react';
    import { View, Button } from 'react-native';
    import Recaptcha from 'react-native-recaptcha-that-works';
    
    const App = () => {
        const recaptcha = useRef();
    
        const send = () => {
            recaptcha.current.open();
        }
    
        const onVerify = token => {
            console.log('success!', token);
        }
    
        const onExpire = () => {
            console.warn('expired!');
        }
    
        return (
            <View>
                <Recaptcha
                    ref={recaptcha}
                    siteKey="<your-recaptcha-public-key>"
                    baseUrl="http://my.domain.com"
                    onVerify={onVerify}
                    onExpire={onExpire}
                    size="invisible"
                />
                <Button title="Send" onPress={send} />
            </View>
        );
    }

    TypeScript Example

    import React, { useRef } from 'react';
    import { View, Button } from 'react-native';
    import Recaptcha, { RecaptchaRef } from "react-native-recaptcha-that-works";
    
    export const Component: React.FC = () => {
        const recaptcha = useRef<RecaptchaRef | null>(null);
    
        const send = () => {
            recaptcha.current?.open();
        };
    
        const onVerify = (token: string) => {
            console.log('success!', token);
        };
    
        const onExpire = () => {
            console.warn('expired!');
        }
    
        return (
            <View>
                <Recaptcha
                    ref={recaptcha}
                    siteKey="<your-recaptcha-public-key>"
                    baseUrl="http://my.domain.com"
                    onVerify={onVerify}
                    onExpire={onExpire}
                    size="invisible"
                />
                <Button title="Send" onPress={send} />
            </View>
        );
    };
    import React, { useRef } from 'react';
    import { View, Button } from 'react-native';
    
    import Recaptcha from 'react-native-recaptcha-that-works';
    
    const App = () => {
        const recaptcha = useRef();
    
        const send = () => {
            recaptcha.current.open();
        }
    
        const onVerify = token => {
            console.log('success!', token);
        }
    
        const onExpire = () => {
            console.warn('expired!');
        }
    
        return (
            <View>
                <Recaptcha
                    ref={recaptcha}
                    siteKey="<your-recaptcha-public-key>"
                    baseUrl="http://my.domain.com"
                    onVerify={onVerify}
                    onExpire={onExpire}
                    size="invisible"
                />
                <Button title="Send" onPress={send} />
            </View>
        );
    };
  3. Troubleshoot invalid siteKey or baseUrl

    master

    Because this package uses the web version of reCAPTCHA v2, ensure the following:

    1. siteKey: Use the site public key from the reCAPTCHA console. Do not use the Android-specific key.
    2. baseUrl: This must be a valid domain that was explicitly added to the Domains section in your reCAPTCHA console setup.

    Example configuration:

    <Recaptcha 
       siteKey="abc123"
       baseUrl="http://mysite.com"
    />
  4. Recaptcha Props Reference

    master

    The Recaptcha component accepts the following props:

    NameTypeDefaultDescription
    siteKeystringRequiredYour Web reCAPTCHA site key. (The Web key must be used, not for Android)
    baseUrlstringRequiredThe URL (domain) configured in the reCAPTCHA console setup. (ex. http://my.domain.com)
    onVerifyfunction(token)RequiredCallback executed when the user submits a successful response. The token is passed to the callback.
    size'invisible'|'normal'|'compact''normal'The size of the widget.
    theme'dark'|'light''light'The color theme of the widget.
    langstring-Language code. If not set, device language is used.
    enterprisebooleanfalseUse the reCAPTCHA Enterprise API. Note: you must choose the size when creating the siteKey for enterprise.
    actionstring-Additional parameter for specifying the action name (Enterprise only).
    onExpirefunction()-Callback when the response expires. Only works if the webview is still open after onVerify has been called for a long time.
    onErrorfunction(error)-Callback when reCAPTCHA encounters an error (e.g. network connectivity).
    onLoadfunction()-Callback executed when reCAPTCHA is ready to use.
    onClosefunction()-Callback executed when the Modal is closed.
    hideBadgebooleanfalseWhen size='invisible', allows hiding the badge if branding is included visibly in the flow.
    styleViewStyle-Customize default style such as background color.
    modalPropsModalProps-Override the Modal props.
    webViewPropsWebViewProps-Override the WebView props.
    headerComponentReact Element-Component to render on top of Modal.
    footerComponentReact Element-Component to render on bottom of Modal.
    loadingComponentReact Element-A custom loading component.
    recaptchaDomainstringwww.google.comThe host name of the reCAPTCHA valid api.
    gstaticDomainstringwww.gstatic.comCustomize reCAPTCHA gstatic host.
  5. Configure the Recaptcha component

    master

    The Recaptcha component is the primary interface for integrating reCAPTCHA into your React Native application. It renders a Modal containing a WebView to handle the reCAPTCHA flow.

    Required Props

    • siteKey: Your Web reCAPTCHA site key. Note: You must use the Web key, not the Android key.
    • baseUrl: The URL (domain) configured in your reCAPTCHA console setup (e.g., http://my.domain.com).
    • onVerify: A callback function executed when the user successfully submits a response. It receives the reCAPTCHA response token as a string.

    Optional Props

    • size: The size of the widget. Options: 'normal' (default) or 'invisible'.
    • theme: The color theme. Options: 'light' (default) or 'dark'.
    • lang: Language code for the reCAPTCHA interface.
    • enterprise: Boolean to enable the reCAPTCHA Enterprise API.
    • action: An additional parameter for specifying the action name (required for reCAPTCHA Enterprise).
    • hideBadge: Boolean to hide the reCAPTCHA badge (only applicable when size='invisible').
    • headerComponent / footerComponent: Custom React components to render at the top or bottom of the Modal.
    • loadingComponent: A custom component to show while the reCAPTCHA is loading.
    • modalProps: Overrides for the underlying React Native Modal component.
    • webViewProps: Overrides for the underlying react-native-webview component.
    • onLoad: Callback executed when reCAPTCHA is ready.
    • onExpire: Callback executed when the response expires.
    • onError: Callback executed when a network or other error occurs.
    • onClose: Callback executed when the Modal is closed.
    • recaptchaDomain: The host name of the reCAPTCHA API. Default: www.google.com.
    • gstaticDomain: Customize the gstatic host. Default: www.gstatic.com.
    <Recaptcha
      siteKey="YOUR_SITE_KEY"
      baseUrl="https://yourdomain.com"
      onVerify={(token) => {
        console.log("Verification token:", token);
      }}
    />
  6. Use the Recaptcha component

    master

    The Recaptcha component is the primary entrypoint for integrating reCAPTCHA into your React Native application. It is exported as the default export from the package. You can control the component via props defined in RecaptchaProps and interact with it imperatively using a ref of type RecaptchaRef.

    import Recaptcha from 'react-native-recaptcha-that-works';
    import { useRef } from 'react';
    import { RecaptchaRef } from 'react-native-recaptcha-that-works';
    
    const MyComponent = () => {
      const recaptchaRef = useRef<RecaptchaRef>(null);
    
      const handleVerify = (token: string | null) => {
        console.log('reCAPTCHA token:', token);
      };
    
      return (
        <Recaptcha
          ref={recaptchaRef}
          siteKey="YOUR_SITE_KEY"
          baseUrl="https://your-base-url.com"
          onVerify={handleVerify}
        />
      );
    };
  7. Control Recaptcha using RecaptchaRef

    master

    To programmatically trigger the reCAPTCHA flow, use a ref attached to the Recaptcha component. The ref provides the following methods:

    • open(): Opens the reCAPTCHA Modal.
    • close(): Closes the reCAPTCHA Modal.
    import React, { useRef } from 'react';
    import Recaptcha, { RecaptchaRef } from 'react-native-recaptcha-that-works';
    
    const MyComponent = () => {
      const recaptchaRef = useRef<RecaptchaRef>(null);
    
      const handlePress = () => {
        recaptchaRef.current?.open();
      };
    
      return (
        <>
          <Button title="Verify me" onPress={handlePress} />
          <Recaptcha
            ref={recaptchaRef}
            siteKey="YOUR_SITE_KEY"
            baseUrl="https://yourdomain.com"
            onVerify={(token) => console.log(token)}
          />
        </>
      );
    };