0.1.0 • Published 4 years ago

react-native-behavauth-sdk v0.1.0

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
4 years ago

image

React Native SDK

This package helps you integrate Quadible's behavioural authentication into a React Native app.

Installation

npm

npm i -S @quadible/react-native-behavauth-sdk

Usage

Typescript is supported out of the box (no need for @types).

import BehavauthSdk from '@quadible/react-native-behavauth-sdk';

const apiKey = '<api-key>';
const apiSecret = '<api-secret>';
const mode = 2; // High Security:0, User Experience: 1, Passive mode: 2
const userId = '<user-id-from-your-organisation>';

(async () => {
    await BehavauthSdk.install(apiKey, apiSecret, mode);

    // Start data collection and authentication
    BehavauthSdk.start(userId);

    // Later, ask the service to authenticate the user
    const result = await BehavauthSdk.authenticate();

    // Change user id if needed
    BehavauthSdk.stop();
    BehavauthSdk.start('<app-user-id>');

    // Clear the session (e.g. when the user logs out)
    BehavauthSdk.stop();

    // Listen for errors and warnings
    client.on('error', (error) => console.log(error));
    client.on('warning', (warning) => console.log(warning));
})();

Listen to RCTEventEmitter to get events regarding Registraction, Authentication and Deletion result.

React.useEffect(() => {
    const eventEmitter = new NativeEventEmitter(
      NativeModules.BehavauthSdkModule
    );
    const eventRegistrationListener = eventEmitter.addListener(
      'onRegistration',
      (event) => {
        if (event.result === 'success') {
          console.log('onRegistration: success - ' + event.message);
        } else {
          console.log('onRegistration: failure - ' + event.message);
        }
      }
    );

    const eventAuthListener = eventEmitter.addListener(
      'onAuthentication',
      (event) => {
        if (event.result === 'success') {
          console.log('onAuthentication: success - ' + event.message);
        } else {
          console.log('onAuthentication: failure - ' + event.message);
        }
      }
    );

    return () => {
      // Removes the listeners for BehavAuth
      eventAuthListener.remove();
      eventRegistrationListener.remove();
    };
), []});

To deploy BehavAuthSDK across the whole session of the app, meaning when the app is on foreground and stop it when the app goes in the background.

import { AppState, AppStateStatus, StyleSheet, View, Text } from 'react-native';
import { NativeEventEmitter, NativeModules } from 'react-native';

export default function App() {
  const [result, setResult] = React.useState<number | undefined>();
  const appState = React.useRef(AppState.currentState);
  const [appStateVisible, setAppStateVisible] = React.useState(
    appState.current
  );
  console.log('Current state: ' + appStateVisible);

  React.useEffect(() => {
    AppState.addEventListener('change', _handleAppStateChange);

    return () => {

      // Removes the listener for App state
      AppState.removeEventListener('change', _handleAppStateChange);
    };
  }, []);

  const _handleAppStateChange = (nextAppState: AppStateStatus) => {
    if (
      appState.current.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {
      console.log('App has come to the foreground!');
      BehavauthSdk.setCallback();
      BehavauthSdk.start(userId);
    } else if (
      appState.current === 'active' &&
      nextAppState.match(/inactive|background/)
    ) {
      console.log('App has come to background');
      BehavauthSdk.stop();
    }

    appState.current = nextAppState;
    setAppStateVisible(appState.current);
    console.log('AppState', appState.current);
  };

  return (
    // ...
  );
}
0.1.0

4 years ago