npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-recaptcha-enterprise

v0.2.7

Published

Google Enterprise reCaptcha library for React-Native. Real native implementation for iOS and Android.

Downloads

251

Readme

react-native-recaptcha-enterprise

Google Enterprise reCaptcha. Implemented through native platforms. Support iOS and Android

Package publish


Installation

  • Requires React Native version 0.60.0, or later.
  • Supports iOS 14.0, or later,
  • Supports android 23 level, or later.

via NPM

npm install react-native-recaptcha-enterprise

via Yarn

yarn add react-native-recaptcha-enterprise

:iphone:iOS (Extra steps)

  • Since ios project is written in Swift, you need to have Swift enabled in your iOS project. If you already have any .swift files, you are good to go. Otherwise, create a new empty Swift source file in Xcode, and allow it to create the neccessary bridging header when prompted.
  • pod install

for react-native 0.71.xx and higher

Since 0.71 version of RN in pod-configuration is enabled to mix-up static and dynamic frameworks. You can configure static frameworks globally (as current package requires that) via

use_frameworks! :linkage => :static

or override this field for specific pod like this

pod 'react-native-recaptcha-enterprise', :path => "../node_modules/react-native-permissions", :linkage => :static, :modular_headers => true

You can see an example in Example/ios/Podfile

Example

In example folder there is simple workflow how to:

  • Initialize reCaptcha client
  • Execute actions
  • Check availability
  • Handle errors

How it looks like you can see here

Methods

Important: All methods are promise based and can throw exceptions. List of errors is provided in Error handling section

Initialize ReCaptcha

It is better to call it somewhere in the startup and use as Singleton instance further

import { initializeRecaptcha } from 'react-native-recaptcha-enterprise';

await initializeRecaptcha('YOUR_SITE_KEY');

Execute

This method works only on initialized reCaptcha client see initialization upper.

There is only one constant action-name - LOGIN. Another as VIEWED_CART, SIGNIN could be added by request. You can pass any custom action name here, but it will be prefixed with string constant custom_

import { executeAction } from 'react-native-recaptcha-enterprise';

const executeResult = await executeAction('YOUR_ACTION_NAME');
console.info('Token verify: ', executeResult);

Check availability

In some cases this library doesn't work on native level. The reason why it happens - missing Google Play Service (gms package). It could be a real case of you end users as some smartphones doesn't have Google environment at all as Xiaomi, Huawei etc. So it is recomended to check it before, or you can handle error in initialization handler by:

errorCode === RecaptchaErrorCodes.NotAvailable

As availability check is called on native part during initialization too.

Implementation of direct method call:

const checkAvailability = useCallback(async () => {
  try {
    setInProgress(true);
    const canUseResult = await canUseRecaptcha();

    if (canUseResult.result) {
      setCanUse(true);
      return;
    }

    setCanUse(false);
    Alert.alert(
      'ReCaptcha Availability',
      'Google ReCaptcha can not be used. Reason: ' + canUseResult.reason
    );
  } catch (e: any) {
    setError(`${e?.message}[code: ${e?.code}]`);
  } finally {
    setInProgress(false);
  }
}, []);

Error handling:

List of supported events:

export enum RecaptchaErrorCodes {
  'NotInitializedClient' = 'NotInitializedClient',
  'NotAvailable' = 'NotAvailable',
  /* Android: UNKNOWN_ERROR, ios: RecaptchaErrorCodeUnknown */
  'RecaptchaErrorCodeUnknown' = 0,
  /* Android: NETWORK_ERROR, ios: RecaptchaErrorNetworkError */
  'RecaptchaErrorNetworkError' = 1,
  /* Android: INVALID_SITEKEY, ios: RecaptchaErrorInvalidSiteKey */
  'RecaptchaErrorInvalidSiteKey' = 2,
  /* Android: INVALID_KEYTYPE, ios: RecaptchaErrorInvalidKeyType */
  'RecaptchaErrorInvalidKeyType' = 3,
  /* Android: INVALID_PACKAGE_NAME, ios: RecaptchaErrorInvalidPackageName */
  'RecaptchaErrorInvalidPackageName' = 4,
  /* Android: INVALID_ACTION, ios: RecaptchaErrorInvalidAction */
  'RecaptchaErrorInvalidAction' = 5,
  /* Android: INTERNAL_ERROR, ios: RecaptchaErrorCodeInternalError */
  'RecaptchaErrorCodeInternalError' = 100,
}

All throwable exception from native part has common object structure:

export type RecaptchaErrorType = {
  code: RecaptchaErrorCodes;
  message: string;
  rawData: any;
};

Where:

  • code - is error code constant (in human-readable format)
  • message - description of error, to define what happened.
  • rawData - whole error object thrown from native part.

You can handle it with Promise.catch().then() approach - classical Promise resolving or use modern one - async/await in combination with try/catch statement.

Short example:

try {
  setInProgress(true);
  const executeResult = await executeAction(actionName);
  setToken(executeResult);
} catch (e: any) {
  // Where: e - is RecaptchaErrorType
  console.log(`${e?.message}[code: ${e?.code}]`);
} finally {
  setInProgress(false);
}

Contributing

See the contributing guide guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library