How to Use the CafSmartAuth Native Module in React Native
This detailed guide explains how to integrate the CafSmartAuth expo module into your Expo project using TypeScript. It includes creating a module and a custom hook to manage events and methods provided by the native SDK.
1. Create the CafSmartAuthBridgeModule.ts File
This file imports the expo module.
Example Implementation
import { NativeModule, requireNativeModule } from "expo";
import { CafSmartAuthBridgeModuleEvents } from "./CafSmartAuthBridgeModule.types";
declare class CafSmartAuthBridgeModule extends NativeModule<CafSmartAuthBridgeModuleEvents> {
startSmartAuth(
mfaToken: string,
faceAuthToken: string,
personId: string,
policyId: string,
jsonString: string
): Promise<void>;
requestLocationPermissions(): Promise<void>;
}
// This call loads the native module object from the JSI.
export default requireNativeModule<CafSmartAuthBridgeModule>(
"CafSmartAuthBridgeModule"
);
2. Create the useSmartAuth.ts Hook
This hook manages the events and state associated with the CafSmartAuth module.
Key Functions
formattedOptions: Formats the settings sent to the native module into JSON format.
useSmartAuth: Hook that:
Listens to events emitted by the native module.
Updates React state based on the events.
startSmartAuth: Method to initiate authentication using the native module.
requestLocationPermissions: Method to request location permissions from the user.