naoufal / react-native-touch-id

React Native authentication with the native Touch ID popup.
https://www.npmjs.com/package/react-native-touch-id
1.47k stars 476 forks source link

How to catch LAErrorUserCancel ? #228

Closed jorisw closed 5 years ago

jorisw commented 5 years ago

I'm using react-native-touch-id 4.4.1 against react-native 0.57.3 in the iOS Simulator.

When a user fails to match biometrics, then cancels the Face ID (in my case) prompt, an error is thrown that makes React Native show a full screen red stack trace, that I can't seem to catch:

LAErrorUserCancel: Authentication was canceled by the user—for example, the user tapped Cancel in the dialog.

How do I catch this error to handle it gracefully?

A code snippet:

handleVerifyBio(promptMessage) {
   try {
      const userBioAuthenticated = await Biometrics.authenticate(promptMessage, {});

      console.log({userBioAuthenticated}); // not printed

      if(userBioAuthenticated){
        return true;
      }

    } catch (error) {
      console.error(error); // not printed
    }
}
jorisw commented 5 years ago

To answer my own question: react-native-touch-id can only be used with the .then() syntax, or you won't properly catch thrown errors.