Skip to main content
React Native

Integrate the Human Token Journey into your React Native app via a native module.

Markdown

The integration uses a native module that delegates to ASWebAuthenticationSession on iOS and Custom Tabs on Android. When the journey finishes, the native module resolves the promise with the callback URL.

Prerequisites​

  • React Native 0.71 or later
  • Xcode 14+ for iOS
  • Android Studio for Android
  • A unique URL scheme for your app (e.g. myapp)

Step 1: Define your URL scheme​

Choose a unique scheme (e.g. myapp). This is used as the callbackUri when creating the process:

callbackUri: myapp://callback

Step 2: Create the TypeScript bridge​

// src/modules/UnicoModule.ts
import { NativeModules } from 'react-native';

const { UnicoModule } = NativeModules;

if (!UnicoModule) {
throw new Error('UnicoModule not found. Make sure the native module is linked correctly.');
}

export const Unico = {
openSession: (url: string): Promise<string> => UnicoModule.openSession(url),
};

Step 3: Implement the iOS native module​

// ios/UnicoModule.m
#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(UnicoModule, NSObject)
RCT_EXTERN_METHOD(openSession:(NSString *)url
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
@end

Step 4: Implement the Android native module​

Add the dependency to android/app/build.gradle:

implementation("androidx.browser:browser:1.5.0")
// android/app/src/main/java/<your.package>/UnicoModule.kt
package com.yourapp

import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import com.facebook.react.bridge.*

class UnicoModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

private var pendingPromise: Promise? = null

override fun getName() = "UnicoModule"

@ReactMethod
fun openSession(url: String, promise: Promise) {
pendingPromise = promise
currentActivity?.runOnUiThread {
CustomTabsIntent.Builder().build()
.launchUrl(currentActivity!!, Uri.parse(url))
} ?: promise.reject("NO_ACTIVITY", "No current activity")
}

fun handleRedirectUrl(uri: Uri) {
if (uri.scheme == "myapp") {
pendingPromise?.resolve(uri.toString())
pendingPromise = null
}
}
}
// UnicoPackage.kt
package com.yourapp

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager

class UnicoPackage : ReactPackage {
override fun createNativeModules(ctx: ReactApplicationContext): List<NativeModule> =
listOf(UnicoModule(ctx))
override fun createViewManagers(ctx: ReactApplicationContext): List<ViewManager<*, *>> =
emptyList()
}

Register in MainApplication.kt:

override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
add(UnicoPackage())
}

Handle redirects in MainActivity.kt:

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
intent.data?.let { uri ->
if (uri.scheme == "myapp") {
val module = reactInstanceManager
.currentReactContext
?.getNativeModule(UnicoModule::class.java)
module?.handleRedirectUrl(uri)
}
}
}

Step 5: Configure AndroidManifest.xml​

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />

<activity android:name=".MainActivity" ...>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="callback" />
</intent-filter>
</activity>

Step 6: Launch the journey​

import { Unico } from './modules/UnicoModule';

const resultUrl = await Unico.openSession(processLink);
// resultUrl = myapp://callback?id=...

File summary​

FilePurpose
src/modules/UnicoModule.tsTypeScript bridge
ios/UnicoModule.mObjective-C declaration
ios/UnicoModule.swiftiOS — ASWebAuthenticationSession
android/.../UnicoModule.ktAndroid — Custom Tabs
android/.../UnicoPackage.ktPackage registration
android/.../MainApplication.ktPackage integration
android/.../MainActivity.ktRedirect URL handling
AndroidManifest.xmlIntent filter