Search code examples
react-nativeexpofingerprintbiometrics

Loop expo biometric authentication until success


I'm trying to implement a biometric authentication (faceID / fingerprint) on Android using React-native with Expo.

Using the LocalAuthentication.authenticateAsync() function, the user is able to authenticate with his biometry. But if it fail, the user have to press the biometric authentication again.

So i tried a little trick with a recursif or do while loop but the result is strange :

const scanFingerPrint = async () => {
        try {
            const results = await DeviceService.biometricAuthentication();
            if (results.success) {
                SecureStoreService.getCredential()
                    .then(credentials => onScan(credentials));
            } else {
                ShakeAnimation(animatedValueModal);
                return scanFingerPrint();
            }
        } catch (e) {
            console.log(e);
        }
    };

With this code, if the user fail the biometric authentication, it will pass in the "else" infinitly...

So I was wondering how to handle that on android.


Solution

  • Expo provide an "error" key on the results of the local authentication. To not handle an hardware error i used this :

    if (!results.success) {
                    switch (results.error) {
                        case "lockout":
                            setLocked(true);
                            break;
                        case "authentication_failed" || "too_fast":
                            ShakeAnimation(animatedValueModal);
                            await scanBiometric();
                            break;
                        case "user_cancel" :
                            break;
                        default:
                            ShakeAnimation(animatedValueModal);
                            break;
                    }
                }