React Native integration (recommended)
You can integrate the SDK into a React Native project to integrate the HUMAN React Native wrapper into a React Native application.
Integrate the HUMAN React Native wrapper
- In the terminal from the command line, run:
- If your app uses iOS, navigate to the
iosfolder and install pods.
Start the SDK
When starting the SDK, start the SDK as soon as possible, and only once. Call startWithAppId in your main entry point (App.tsx) or where you register and initialize one-time services. Start the SDK outside of any UI component to avoid lifecycle issues. If your app sends a URL request to your server before the SDK starts, the attached headers won’t include the latest updated tokens. As a result:
- The Enforcer may block the request, and
- The SDK will not be able to present a challenge to the user
Below is an example implementation:
Don’t forget to replace <APP_ID> with your actual Application ID.
In particular, note the following:
-
Initialization: We start the SDK as early as possible and only once. You can move this code to a different function or file and export it as long as it’s only called once.
-
Policy configuration: An optional
HSPolicyclass can configure the SDK’s behavior. This object defines settings such ashybridAppPolicy.webRootDomains. This class has two settings:hybridAppPolicy: Use this if your app operates in hybrid mode, meaning it has a WebView that loads a site that’s also protected by HUMAN and running our Sensor. In this case, configure all relevant website domains with the Application ID. Hybrid is supported forreact-native-webviewwhenwebRootDomainsis set. SetwebRootDomainsto the bare registrable root (example.com). A leading dot is accepted and equivalent. Do not listwwwor a subdomain. This wrapper turnssupportExternalWebViewson. See WebView container support for more information.detectionPolicy: This setting lets you configure whether the SDK can send touch and motion events, either touch only or both touch and motion, to the server to improve detection. By default, it’s off, and you can choose to enable it.
-
Starting the SDK: We call
startWithAppIdwith the following parameters:- Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The policy object, if needed.
Integrate with Bot Defender
Send your requests through the SDK and check for a blocked request
- Your application adds the SDK’s HTTP headers to the URL request. Include them on every request, and do not cache them. They contain a token with an expiration date, and the SDK keeps that token up to date. Each request should call the HumanSecurity module so the headers are not reused from a previous call.
- Your application sends the URL request.
- If the response is blocked, pass the response body to the SDK. Call
canHandleResponse. If it returns true, callhandleResponseto present the challenge. Pass the arguments correctly. When using Axios, callJSON.stringify(error.response?.data)before passing the response body, and handle the blocked response in thecatchblock because Axios treats it as a failed request. handleResponseresolves toHSBotDefenderChallengeResult:SOLVED: The user successfully completed the challenge.CANCELED: The user dismissed or failed to complete the challenge.FAILED: The SDK was unable to handle the response, usually indicating that something wasn’t passed correctly tohandleResponse.
- You can await
handleResponse, which waits until the user solves the challenge. You can also continue your flow and handle the result in the.thenblock.
Below are example implementations using the Fetch API or Axios:
Handle blocked requests
When the Enforcer blocks a request, it returns a JSON string in the response body with an HTTP status code of 403, which includes metadata for the SDK. For example:
Your app should pass the entire JSON as response in a string format to HumanSecurity.BD.handleResponse. If not, the SDK won’t present a challenge to the user.
When handling blocked requests:
- Treat blocked requests as a failure. Your app’s UI is shown again after the user solves or cancels the challenge. If the request was triggered by a user action, make it clear that the user may try the same action again.
- Use the promise result to log analytics, create logs, and so on.
- (Optional) Use the returned promise to retry the original request when appropriate. Consider the following:
- Promise scope: The promise returns outside the original request’s scope. Ensure your app handles this case correctly.
- Challenge cancellation: The user might cancel the challenge. Do not retry the request to avoid it being blocked again.
- Retry block possibility: The retry attempt may also be blocked. Do not assume it will succeed.
Set custom parameters (optional)
Custom parameters are additional custom values that you can set yourself to enrich the data you receive from HUMAN. You can have up to 10 custom parameters, and they must follow the pattern custom_paramN, where N is a number between 1 and 10. Call HumanSecurity.BD.setCustomParameters only after startWithAppId has been called.
Below is an example implementation:
Subscribe to events (optional)
Bot Defender provides several delegates, or callbacks, to notify about events related to headers and challenges. These callbacks are mainly intended for analytics and statistics rather than for taking specific actions. In the Expo library, these callbacks work as events.
To listen for Bot Defender events, subscribe to the onBotDefenderEvent listener. The received event will be of type BotDefenderEvent and can be handled as needed.
BotDefenderEvent structure and event options
BotDefenderEvent structure and event options
Only the botDefenderDidUpdateHeaders event provides headers. The rest provide the Application ID.
Integrate with Account Defender
Enable Account Defender in your app
To enable Account Defender, set the UserID of your current logged-in user in the SDK. When the user logs out, call the function with null to clear the user ID.
Below is an example implementation:
Notify HUMAN's backend on outgoing URL requests from the app
For Account Defender to protect the user’s account, your app has to provide the SDK with outgoing URL requests. Do this after you set the user ID. Call HumanSecurity.AD.registerOutgoingUrlRequest(url: string, appId?: string) before you send the URL request. If you use a custom HTTP client, add this function to the request interceptor.
Below is an example implementation:
Set additional data (optional)
Additional data are additional custom values that you can set yourself to enrich the data you receive from HUMAN. These are separate from custom parameters and are unique to Account Defender. Call HumanSecurity.AD.setAdditionalData(parameters: { [key: string]: string }, appId?: string) only after HumanSecurity.startWithAppId(appId: string, policy?: HSPolicy) has been called.
Below is an example implementation:
HUMAN React Native API reference
The HUMAN Security React Native Wrapper API offers the following functions: