Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-native-biometrics with all npm packages installed. Try it out:

require("react-native/package.json"); // react-native is a peer dependency. var reactNativeBiometrics = require("react-native-biometrics")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-native-biometrics v2.1.4

React Native biometric functionality for signing and encryption

react-native-biometrics

React native biometrics is a simple bridge to native iOS and Android keystore management. It allows you to create public private key pairs that are stored in native keystores and protected by biometric authentication. Those keys can then be retrieved later, after proper authentication, and used to create a cryptographic signature.

React Native Compatibility

react-native-biometrics versionRequired React Native Version
>= 2.0.0>= 0.60
<= 1.7.0<= 0.59.x

Getting started

using either Yarn:

yarn add react-native-biometrics

or npm:

$ npm install react-native-biometrics --save

Link / Autolinking

On React Native 0.60+ the CLI autolink feature links the module while building the app.

Additional configuration

iOS

This package requires an iOS target SDK version of iOS 10 or higher

Ensure that you have the NSFaceIDUsageDescription entry set in your react native iOS project, or Face ID will not work properly. This description will be will be presented to the user the first time a biometrics action is taken, and the user will be asked if they want to allow the app to use Face ID. If the user declines the usage of face id for the app, the isSensorAvailable function will indicate biometrics is unavailable until the face id permission is specifically allowed for the app by the user.

Android

This package requires a compiled SDK version of 29 (Android 10.0) or higher

Usage

This package is designed to make server authentication using biometrics easier. Here is an image from https://android-developers.googleblog.com/2015/10/new-in-android-samples-authenticating.html illustrating the basic use case:

react-native-biometrics

When a user enrolls in biometrics, a key pair is generated. The private key is stored securely on the device and the public key is sent to a server for registration. When the user wishes to authenticate, the user is prompted for biometrics, which unlocks the securely stored private key. Then a cryptographic signature is generated and sent to the server for verification. The server then verifies the signature. If the verification was successful, the server returns an appropriate response and authorizes the user.

Constants

TouchID (iOS only)

A constant for the touch id sensor type, evaluates to 'TouchID'

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const { biometryType } = await ReactNativeBiometrics.isSensorAvailable()

if (biometryType === ReactNativeBiometrics.TouchID) {
  //do something fingerprint specific
}

FaceID (iOS only)

A constant for the face id sensor type, evaluates to 'FaceID'

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const { biometryType } = await ReactNativeBiometrics.isSensorAvailable()

if (biometryType === ReactNativeBiometrics.FaceID) {
  //do something face id specific
}

Biometrics (Android only)

A constant for generic Biometrics, evaluates to 'Biometrics'

Example

import ReactNativeBiometrics from 'react-native-biometrics'

const { biometryType } = await ReactNativeBiometrics.isSensorAvailable()

if (biometryType === ReactNativeBiometrics.Biometrics) {
  //do something face id specific
}

Methods

isSensorAvailable()

Detects what type of biometric sensor is available. Returns a Promise that resolves to an object with details about biometrics availability

Result Object

PropertyTypeDescription
availableboolA boolean indicating if biometrics is available or not
biometryTypestringA string indicating what type of biometrics is available. TouchID, FaceID, Biometrics, or undefined if biometrics is not available.
errorstringAn error message indicating why biometrics may not be available. undefined if there is no error.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.isSensorAvailable()
  .then((resultObject) => {
    const { available, biometryType } = resultObject

    if (available && biometryType === ReactNativeBiometrics.TouchID) {
      console.log('TouchID is supported')
    } else if (available && biometryType === ReactNativeBiometrics.FaceID) {
      console.log('FaceID is supported')
    } else if (available && biometryType === ReactNativeBiometrics.Biometrics) {
      console.log('Biometrics is supported')
    } else {
      console.log('Biometrics not supported')
    }
  })

createKeys()

Generates a public private RSA 2048 key pair that will be stored in the device keystore. Returns a Promise that resolves to an object providing details about the keys.

Result Object

PropertyTypeDescription
publicKeystringA base64 encoded string representing the public key

Example

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.createKeys('Confirm fingerprint')
  .then((resultObject) => {
    const { publicKey } = resultObject
    console.log(publicKey)
    sendPublicKeyToServer(publicKey)
  })

biometricKeysExist()

Detects if keys have already been generated and exist in the keystore. Returns a Promise that resolves to an object indicating details about the keys.

Result Object

PropertyTypeDescription
keysExistboolA boolean indicating if keys exist in the keystore

Example

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.biometricKeysExist()
  .then((resultObject) => {
    const { keysExist } = resultObject

    if (keysExist) {
      console.log('Keys exist')
    } else {
      console.log('Keys do not exist or were deleted')
    }
  })

deleteKeys()

Deletes the generated keys from the device keystore. Returns a Promise that resolves to an object indicating details about the deletion.

Result Object

PropertyTypeDescription
keysDeletedboolA boolean indicating if keys were deleted from the keystore

Example

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.deleteKeys()
  .then((resultObject) => {
    const { keysDeleted } = resultObject

    if (keysDeleted) {
      console.log('Successful deletion')
    } else {
      console.log('Unsuccessful deletion because there were no keys to delete')
    }
  })

createSignature(options)

Prompts the user for their fingerprint or face id in order to retrieve the private key from the keystore, then uses the private key to generate a RSA PKCS#1v1.5 SHA 256 signature. Returns a Promise that resolves to an object with details about the signature.

**NOTE: No biometric prompt is displayed in iOS simulators when attempting to retrieve keys for signature generation, it only occurs on actual devices.

Options Object

ParameterTypeDescriptioniOSAndroid
promptMessagestringMessage that will be displayed in the fingerprint or face id prompt
payloadstringString of data to be signed by the RSA signature
cancelButtonTextstringText to be displayed for the cancel button on biometric prompts, defaults to Cancel

Result Object

PropertyTypeDescription
successboolA boolean indicating if the process was successful, false if the users cancels the biometrics prompt
signaturestringA base64 encoded string representing the signature. undefined if the process was not successful.
errorstringAn error message indicating reasons why signature creation failed. undefined if there is no error.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

let epochTimeSeconds = Math.round((new Date()).getTime() / 1000).toString()
let payload = epochTimeSeconds + 'some message'

ReactNativeBiometrics.createSignature({
    promptMessage: 'Sign in',
    payload: payload
  })
  .then((resultObject) => {
    const { success, signature } = resultObject

    if (success) {
      console.log(signature)
      verifySignatureWithServer(signature, payload)
    }
  })

simplePrompt(options)

Prompts the user for their fingerprint or face id. Returns a Promise that resolves if the user provides a valid biometrics or cancel the prompt, otherwise the promise rejects.

**NOTE: This only validates a user's biometrics. This should not be used to log a user in or authenticate with a server, instead use createSignature. It should only be used to gate certain user actions within an app.

Options Object

ParameterTypeDescriptioniOSAndroid
promptMessagestringMessage that will be displayed in the biometrics prompt
cancelButtonTextstringText to be displayed for the cancel button on biometric prompts, defaults to Cancel

Result Object

PropertyTypeDescription
successboolA boolean indicating if the biometric prompt succeeded, false if the users cancels the biometrics prompt
errorstringAn error message indicating why the biometric prompt failed. undefined if there is no error.

Example

import ReactNativeBiometrics from 'react-native-biometrics'

ReactNativeBiometrics.simplePrompt({promptMessage: 'Confirm fingerprint'})
  .then((resultObject) => {
    const { success } = resultObject

    if (success) {
      console.log('successful biometrics provided')
    } else {
      console.log('user cancelled biometric prompt')
    }
  })
  .catch(() => {
    console.log('biometrics failed')
  })

Troubleshooting

  • Because of this library's dependency on androidx.biometric:biometric:1.0.0 it can cause transitive dependency resolution to change on certain version of React Native and androidx.swiperefreshlayout may no longer be able to be resolved. This can be fixed by adding an explicit dependency on the library in your android/app/build.gradle:

    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "com.facebook.react:react-native:+"  // From node_modules
        implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" // temp fix
        ...
    }
    
  • There is a known issue on the iOS 13.x simulators where keys generated with access control flags cannot be queried and found properly. This results in key not found errors in biometricKeysExist and createSignature on those simulators. However, it works correctly on actual devices running iOS 13.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free