1.0.9 • Published 7 months ago

@valture/react-native-recaptcha-v3 v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

react-native-recaptcha-v3

A React Native component for seamless integration of Google reCAPTCHA v3 into React Native applications.

Installation

Prerequisites

This library depends on react-native-webview. Ensure it’s already installed in your project. If not, install it along with the package:

# Install both packages if WebView is not installed
npm install @valture/react-native-recaptcha-v3 react-native-webview
# or
yarn add @valture/react-native-recaptcha-v3 react-native-webview

If react-native-webview is already installed:

npm install @valture/react-native-recaptcha-v3
# or
yarn add @valture/react-native-recaptcha-v3

Usage

import ReCaptcha, { GoogleRecaptchaRefAttributes } from '@valture/react-native-recaptcha-v3';
import React, { useRef } from 'react';

const YourComponent = () => {
  const recaptchaRef = useRef<GoogleRecaptchaRefAttributes>(null);

  const handleVerify = async () => {
    try {
      const token = await recaptchaRef.current?.getToken('your_action');
      console.log('reCAPTCHA token:', token);
      // Send token to your backend for verification
    } catch (error) {
      console.error('reCAPTCHA error:', error);
    }
  };

  return (
    <ReCaptcha
      ref={recaptchaRef}
      siteKey="your_site_key_here"
      baseUrl="your_domain_here"
      onVerify={(token) => console.log('Verified:', token)}
      onError={(error) => console.error('Error:', error)}
    />
  );
};

Props

PropTypeRequiredDescription
siteKeystringYesYour Google reCAPTCHA v3 site key.
baseUrlstringYesYour domain URL (must match reCAPTCHA configuration).
actionstringNoCustom action name (default: 'submit').
onVerifyfunctionNoCallback function when verification succeeds.
onErrorfunctionNoCallback function when verification fails.
styleViewStyleNoCustom styles for the WebView.
containerStyleViewStyleNoCustom styles for the container.

Methods

getToken(action?: string): Promise<string | null>

Retrieves a new reCAPTCHA token for the given action.
Returns a Promise that resolves with the token string.

Example:

const token = await recaptchaRef.current?.getToken('custom_action');
console.log('Token:', token);

Full Example

import React, { useRef } from 'react';
import { Button, View } from 'react-native';
import ReCaptcha, { GoogleRecaptchaRefAttributes } from '@valture/react-native-recaptcha-v3';

const App = () => {
  const recaptchaRef = useRef<GoogleRecaptchaRefAttributes>(null);

  const handleSubmit = async () => {
    try {
      const token = await recaptchaRef.current?.getToken('login');
      console.log('Token:', token);
      // Send token to your backend
    } catch (error) {
      console.error('reCAPTCHA error:', error);
    }
  };

  return (
    <View style={{ flex: 1 }}>
      <ReCaptcha
        ref={recaptchaRef}
        siteKey="your_site_key_here"
        baseUrl="https://your-domain.com"
        onVerify={(token) => console.log('Verified:', token)}
        onError={(error) => console.error('Error:', error)}
        containerStyle={{position: 'absolute', backfaceVisibility: 'hidden', opacity: 0}}  // to make it invisible
      />
      <Button title="Submit" onPress={handleSubmit} />
    </View>
  );
};

export default App;

Troubleshooting

  • Ensure your siteKey matches the key configured on the Google reCAPTCHA Admin Console.
  • baseUrl should correspond to the domain registered with Google reCAPTCHA.

License

This project is licensed under the MIT License.


Contributing

We welcome contributions! Follow our contributing guide for instructions on how to contribute to this project.

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago