React Native module for iOS and Android biometrics
React Native module for iOS and Android 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-biometrics version |
Required React Native Version |
|---|---|
>= 3.0.0 |
>= 0.60 |
| ` { |
const { available, biometryType } = resultObject
if (available && biometryType === BiometryTypes.TouchID) {
console.log('TouchID is supported')
} else if (available && biometryType === BiometryTypes.FaceID) {
console.log('FaceID is supported')
} else if (available && biometryType === BiometryTypes.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__
| Property | Type | Description |
| --- | --- | --- |
| publicKey | string | A base64 encoded string representing the public key |
__Example__
```js
import ReactNativeBiometrics from 'react-native-biometrics'
const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.createKeys()
.then((resultObject) => {
const { publicKey } = resultObject
console.log(publicKey)
sendPublicKeyToServer(publicKey)
})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
| Property | Type | Description |
|---|---|---|
| keysExist | bool | A boolean indicating if keys exist in the keystore |
Example
import ReactNativeBiometrics from 'react-native-biometrics'
const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.biometricKeysExist()
.then((resultObject) => {
const { keysExist } = resultObject
if (keysExist) {
console.log('Keys exist')
} else {
console.log('Keys do not exist or were deleted')
}
})Deletes the generated keys from the device keystore. Returns a Promise that resolves to an object indicating details about the deletion.
Result Object
| Property | Type | Description |
|---|---|---|
| keysDeleted | bool | A boolean indicating if keys were deleted from the keystore |
Example
import ReactNativeBiometrics from 'react-native-biometrics'
const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.deleteKeys()
.then((resultObject) => {
const { keysDeleted } = resultObject
if (keysDeleted) {
console.log('Successful deletion')
} else {
console.log('Unsuccessful deletion because there were no keys to delete')
}
})
…
js
import ReactNativeBiometrics from 'react-native-biometrics'
let epochTimeSeconds = Math.round((new Date()).getTime() / 1000).toString()
let payload = epochTimeSeconds + 'some message'
const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.createSignature({
promptMessage: 'Sign in',
payload: payload
})
.then((resultObject) => {
const { success, signature } = resultObject
if (success) {
console.log(signature)
verifySignatureWithServer(signature, payload)
}
})
…
js
import ReactNativeBiometrics from 'react-native-biometrics'
const rnBiometrics = new ReactNativeBiometrics()
rnBiometrics.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')
})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.
No open issues yet, or sync has not completed.