Ionic Integration
You can integrate the SDK into an Ionic project. There is no HUMAN Ionic package. Call the native SDK from JavaScript through a Capacitor plugin.
Prerequisites
If the app loads a HUMAN-protected site in a Capacitor WebView, set hybridAppPolicy.webRootDomains and hybridAppPolicy.supportExternalWebViews = true in native start. There is no wrapper-level default. Instead, the flag is set manually. See WebView container support for more information.
Start the SDK
When starting the SDK, keep the following in mind:
- Start the SDK as soon as possible on the main thread. We recommend doing so in the
Application’sonCreatefunction on Android or theAppDelegate’sdidFinishLaunchingWithOptionsfunction on iOS. If your app sends a URL request to your server before the SDK starts, the request will not include the SDK’s HTTP headers. As a result:- The Enforcer may block the request, and
- The SDK will not be able to present a challenge to the user
- Create a
HumanManagerthat handles calls from Ionic.
Below are example implementations for Android and iOS:
Don’t forget to replace <APP_ID> with your actual Application ID.
Android
iOS
In particular, note the following:
- Initialization: We start the SDK as early as possible on the main thread.
- Policy configuration: We create an
HSPolicyinstance and set theHSAutomaticInterceptorPolicy/interceptorTypeproperty toHSAutomaticInterceptorType/none. That disables Automatic Interception, which lets the SDK manipulate URL requests and handle their responses, but isn’t supported in Ionic. - Starting the SDK: We call
HumanSecurity/start(appId:policy:)with the following parameters:- The
Applicationinstance (Android only) - Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The
HSPolicyobject that we configured
- The
- Send the response: The response is sent from the Ionic to the native side.
- Handle the response: The native side calls the
HSBotDefender/handleBlockResponsefunction. This function presents a challenge to the user when the provided response is a blocked response.
Send your requests through the SDK and check for a blocked request
- Export the
HumanManagerplugin. - Add the SDK’s HTTP headers to your URL requests.
- When a request is blocked, send the block response to the SDK.
- Handle the challenge’s result. See handle blocked requests for more information.
Below is an example implementation:
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 whole JSON to the SDK via the HSBotDefender/handleBlockResponse function. If not, the SDK won’t present a challenge to the user.