React Native Integration (Legacy)
React Native Integration (Legacy)
React Native Integration (Legacy)
The SDK can be integrated into a React Native project.
This integration is the legacy version. For the most recent version, see our recommended integration.
If your project is built with C++ (AppDelegate.mm), you should do the following:
Apple Clang - Custom Compiler Flags -> Other C++ Flags.-fcxx-modules flag.HumanModule.Add a package declaration at the top of each Java file matching your project’s package name (e.g. package com.yourapp;).
Java / HumanModule.java:
HumanModule to the HumanPackage and set the shared property.Java / HumanPackage.java:
Create a native module called HumanModule.
Objective-C / HumanModule.h:
Objective-C / HumanModule.m:
Application’s onCreate function on Android.AppDelegate’s didFinishLaunchingWithOptions function on iOS.HumanDelegate to receive events from the SDK.Here is an example of how it should be:
Java / MainApplication.java:
Objective-C / AppDelegate.h:
Objective-C / AppDelegate.m:
Don’t forget to change the <APP_ID> to your own AppID and <YOUR_APP_NAME> to your React Native module name (as registered in AppRegistry).
Let’s talk about what we have in the code here:
HSPolicy instance to configure the SDK’s behavior. Here, we set the interceptorType property to HSAutomaticInterceptorType.NONE. This disables the automatic interception feature, which is not supported in React Native.HumanSecurity.start(appId, policy) function of the SDK with:
Application instance (Android only).startObserving function on the HumanModule.SoLoader.init(this, OpenSourceMergedSoMapping.INSTANCE) for proper native library loading (required for React Native 0.73+).AppDelegate extends RCTAppDelegate (required for React Native 0.73+). Set self.moduleName and self.initialProps, then call [super application:application didFinishLaunchingWithOptions:launchOptions] to configure React Native.Note: If your app communicates with multiple servers with different AppIDs, you can call the HumanSecurity.start(appIds, policy) function to pass an array of AppIDs. Specify the relevant AppID for each API call in the SDK.
In your JavaScript code, you should:
HumanModule.JavaScript:
The HUMAN’s Enforcer, when it decides to block a request, returns a JSON string in the response’s body with an HTTP status code of 403. Here is an example of the response:
The JSON contains metadata for the SDK.
Your app should pass the whole JSON to the SDK via the HSBotDefender/handleResponse(response:data:callback:) function. Otherwise, the SDK won’t present a challenge to the user.