> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/caf-sdk/react-native/standalone-modules/cafsmartauth/expo-modules/import-expo-module.md).

# Import Expo Modules

## 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

```ts
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.

#### Example Implementation

```ts
import { useState, useEffect } from "react";

import CafSmartAuthBridgeModule, {
  CafSmartAuthSettings,
  CafSmartAuthResponse,
  CafSmartAuthSuccess,
  CafSmartAuthError,
  CafSmartAuthPending,
  CafSmartAuthCancel,
  CafSmartAuthLoading,
  CafSmartAuthLoaded,
} from "../";

let responseFormattedOptions: string = "";

const formattedOptions = (settings: CafSmartAuthSettings): string => {
  const formatToJSON = JSON.stringify({
    ...settings,
  });

  return formatToJSON;
};

const useSmartAuth = (settings?: CafSmartAuthSettings) => {
  const [response, setResponse] = useState<CafSmartAuthResponse>({
    success: {
      isAuthorized: false,
      attemptId: null,
      attestation: null,
    },
    error: null,
    cancelled: false,
    isLoading: false,
    pending: {
      isAuthorized: false,
      attestation: null,
    },
  });

  responseFormattedOptions = formattedOptions(settings!);

  useEffect(() => {
    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Success",
      (event: CafSmartAuthSuccess) => {
        setResponse({
          success: {
            isAuthorized: event.isAuthorized,
            attemptId: event.attemptId,
            attestation: event.attestation,
          },
          error: null,
          cancelled: false,
          isLoading: false,
          pending: {
            isAuthorized: false,
            attestation: null,
          },
        });
      }
    );

    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Error",
      (event: CafSmartAuthError) => {
        setResponse({
          success: {
            isAuthorized: false,
            attemptId: null,
            attestation: null,
          },
          error: event,
          cancelled: false,
          isLoading: false,
          pending: {
            isAuthorized: false,
            attestation: null,
          },
        });
      }
    );

    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Cancel",
      (event: CafSmartAuthCancel) => {
        setResponse({
          success: {
            isAuthorized: false,
            attemptId: null,
            attestation: null,
          },
          error: null,
          cancelled: event.isCancelled,
          isLoading: false,
          pending: {
            isAuthorized: false,
            attestation: null,
          },
        });
      }
    );

    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Pending",
      (event: CafSmartAuthPending) => {
        setResponse({
          success: {
            isAuthorized: false,
            attemptId: null,
            attestation: null,
          },
          error: null,
          cancelled: false,
          isLoading: false,
          pending: {
            isAuthorized: event.isAuthorized,
            attestation: event.attestation,
          },
        });
      }
    );

    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Loading",
      (event: CafSmartAuthLoading) => {
        setResponse({
          success: {
            isAuthorized: false,
            attemptId: null,
            attestation: null,
          },
          error: null,
          cancelled: false,
          isLoading: event.isLoading,
          pending: {
            isAuthorized: false,
            attestation: null,
          },
        });
      }
    );

    CafSmartAuthBridgeModule.addListener(
      "CafSmartAuth_Loaded",
      (event: CafSmartAuthLoaded) => {
        setResponse({
          success: {
            isAuthorized: false,
            attemptId: null,
            attestation: null,
          },
          error: null,
          cancelled: false,
          isLoading: event.isLoaded,
          pending: {
            isAuthorized: false,
            attestation: null,
          },
        });
      }
    );

    return () => {
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Success");
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Pending");
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Error");
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Cancel");
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Loading");
      CafSmartAuthBridgeModule.removeAllListeners("CafSmartAuth_Loaded");
    };
  }, []);

  return {
    success: response.success,
    error: response.error,
    cancelled: response.cancelled,
    pending: response.pending,
    isLoading: response.isLoading,
  };
};

const startSmartAuth = (
  mfaToken: string,
  faceAuthToken: string,
  policyId: string,
  personId: string
) => {
  CafSmartAuthBridgeModule.startSmartAuth(
    mfaToken,
    faceAuthToken,
    personId,
    policyId,
    responseFormattedOptions
  );
};

const requestLocationPermissions = async () => {
  await CafSmartAuthBridgeModule.requestLocationPermissions();
};

export { startSmartAuth, requestLocationPermissions, useSmartAuth };
```

### 3. Available Methods

* **`useSmartAuth`**: A hook that provides the following states:
  * `success`: Information about successful authentications.
  * `error`: Details about errors that occurred.
  * `cancelled`: Indicates if the operation was cancelled.
  * `pending`: Information about pending authentications.
  * `isLoading`: Indicates if authentication is in progress.
* **`startSmartAuth`**: Method to start the authentication process.
  * **Parameters**:
    * `mfaToken`: Multi-factor authentication token.
    * `faceAuthToken`: Token for facial authentication.
    * `policyId`: ID of the authentication policy.
    * `personId`: ID of the person to authenticate.
* **`requestLocationPermissions`**: Method to request location permissions from the user (Android only).

### 4. Project Integration

Here is how to implement the hook in your project, follow [SourceCode](/caf-sdk/react-native/standalone-modules/cafsmartauth/expo-modules/source-code.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.caf.io/caf-sdk/react-native/standalone-modules/cafsmartauth/expo-modules/import-expo-module.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
