SelfLender / react-native-biometrics

React Native module for iOS and Android biometrics
MIT License
664 stars 228 forks source link

Cannot read property 'isSensorAvailable' of undefined #251

Open sagarmph opened 1 year ago

sagarmph commented 1 year ago

I have encountered this below error after importing the package and trying to use it in my react native application. Request for the solution for below. The error is : Uncaught TypeError: Cannot read property 'isSensorAvailable' of undefined

I have tried with this both snippets from below, But unable to get the promise resolved or get the expected the expected console.

  1. useEffect(() => { const rnBiometrics = new ReactNativeBiometrics(); rnBiometrics.isSensorAvailable().then((resultObject) => { 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'); } }); }, []);

  2. const isBiometricSupport = async () => { let {available, biometryType} = await ReactNativeBiometrics.isSensorAvailable(); if (available && biometryType === ReactNativeBiometrics.TouchID) { console.log('TouchID is supported', biometryType); } else if (available && biometryType === ReactNativeBiometrics.FaceID) { console.log('FaceID is supported', biometryType); } else if (available && biometryType === ReactNativeBiometrics.Biometrics) { console.log('Biometrics is supported', biometryType); } else { console.log('Biometrics not supported', biometryType); } }; useEffect(() => { isBiometricSupport(); }, []);

React native version : >0.60.0 react-native-biometrics: 3.0.1 Trying to run on Android Phone in debug mode.

Zcehtro commented 1 year ago

I'm having the same issue. I tried using the examples provided in the README and then looked up a few tutorials and found the same one @sagarmph is following:

https://blog.logrocket.com/implementing-react-native-biometric-authentication-expo/#Using-react-native-biometrics

I'm always stuck with an unhandled promise rejection.

sagarmph commented 1 year ago

@Zcehtro The problem was with the package linking. I was able to use it when I had done the linking from the description of older version of package https://www.npmjs.com/package/react-native-biometrics/v/1.7.1

Also, Do pass the allowDeviceCredentials: true while initalizing. const rnBiometrics = new ReactNativeBiometrics({ allowDeviceCredentials: true })

Zcehtro commented 1 year ago

Thank you for your reply!

I think the problem in my situation is that I'm writing a React Native app using Expo. And the documentation for react-native-biometrics doesn't indicate if it's compatible with Expo or not.

From what I understand about what you shared, I'd need to run react-native link react-native-biometrics or it's equivalent in Expo... but Expo doesn't allow running react-native link and doesn't offer an equivalent.

How are you fairing with this?

maniteja-emmadi commented 1 year ago

I am getting a similar error saying [TypeError: Cannot read property 'isSensorAvailable' of null] for the exact same code that you've written except that I did not put the code snippet in useEffect I'm calling enableTouchId method on press of a TouchableOpacity

asfaqehussain commented 1 year ago

I got the solution for this.

You have to add allowDeviceCredentials in initialization



import ReactNativeBiometrics, { BiometryTypes } from 'react-native-biometrics'
 const rnBiometrics = new ReactNativeBiometrics({ allowDeviceCredentials: true })

       rnBiometrics.isSensorAvailable()
                .then((resultObject) => {
                    const { available, biometryType } = resultObject
                    console.log(' available ----> ', available);
                    console.log(' biometryType ----> ', biometryType);

                    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')
                    }
                })```
hoanguyen3006 commented 11 months ago

I got the same error above on simulator Ios, but working on simualtor Android.

Screenshot 2023-11-30 at 13 12 17

hoanguyen3006 commented 11 months ago

I got the same error above on simulator Ios, but working on simualtor Android.

Screenshot 2023-11-30 at 13 12 17

I have a solution. add <uses-permission android:name="android.permission.USE_FINGERPRINT"/> to AndroidManifest.xml for ANDROID add <key>NSFaceIDUsageDescription</key> <string>Enabling Face ID allows you quick and secure access to your account.</string> to Info.plist for IOS

FoRavel commented 11 months ago

The FaceID will work only if user has configured it on his device Settings.

If user has not configured is FaceID, it will fallback to the simple passcode verification (the code you enter to unlock your phone) when allowDeviceCredentials: true .

If you did not set allowDeviceCredientals to true and user has not enabled the FaceID on his phone of course, nothing will happen and new ReactNativeBiometrics(); will be undefined.

hoanguyen3006 commented 11 months ago

The FaceID will work only if user has configured it on his device Settings.

If user has not configured is FaceID, it will fallback to the simple passcode verification (the code you enter to unlock your phone) when allowDeviceCredentials: true .

If you did not set allowDeviceCredientals to true and user has not enabled the FaceID on his phone of course, nothing will happen and new ReactNativeBiometrics(); will be undefined.

Thanks for reply, I have noted the problem.