Skip to content

Commit

Permalink
Refactor error message
Browse files Browse the repository at this point in the history
  • Loading branch information
jensdev committed Dec 15, 2022
1 parent 06f6cea commit 4c8e12c
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions android/src/main/java/com/rnbiometrics/ReactNativeBiometrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
resultMap.putString("biometryType", "Fingerprint");
}


promise.resolve(resultMap);
} else if (allowDeviceCredentials) {
int canAuthenticateCredentials = biometricManager.canAuthenticate(getAllowedAuthenticators(true));
Expand All @@ -77,40 +76,19 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
WritableMap resultMap = new WritableNativeMap();
resultMap.putBoolean("available", true);
resultMap.putString("biometryType", "Credentials");

promise.resolve(resultMap);
} else {
WritableMap resultMap = new WritableNativeMap();
resultMap.putBoolean("available", false);

switch (canAuthenticateCredentials) {
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
resultMap.putString("error", "BIOMETRIC_ERROR_NO_HARDWARE");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
resultMap.putString("error", "BIOMETRIC_ERROR_HW_UNAVAILABLE");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
resultMap.putString("error", "BIOMETRIC_ERROR_NONE_ENROLLED");
break;
}
resultMap.putString("error", parseError(canAuthenticateCredentials));

promise.resolve(resultMap);
}
} else {
WritableMap resultMap = new WritableNativeMap();
resultMap.putBoolean("available", false);

switch (canAuthenticate) {
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
resultMap.putString("error", "BIOMETRIC_ERROR_NO_HARDWARE");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
resultMap.putString("error", "BIOMETRIC_ERROR_HW_UNAVAILABLE");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
resultMap.putString("error", "BIOMETRIC_ERROR_NONE_ENROLLED");
break;
}
resultMap.putString("error", parseError(canAuthenticate));

promise.resolve(resultMap);
}
Expand All @@ -121,10 +99,29 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
promise.resolve(resultMap);
}
} catch (Exception e) {
promise.reject("Error detecting biometrics availability: " + e.getMessage(), "Error detecting biometrics availability: " + e.getMessage());
promise.reject("Error detecting biometrics availability: " + e.getMessage(),
"Error detecting biometrics availability: " + e.getMessage());
}
}

private String parseError(final int authenticationResult) {
String message = "";

switch (authenticationResult) {
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
message = "BIOMETRIC_ERROR_NO_HARDWARE";
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
message = "BIOMETRIC_ERROR_HW_UNAVAILABLE";
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
message = "BIOMETRIC_ERROR_NONE_ENROLLED";
break;
}

return message;
}

@ReactMethod
public void createKeys(final ReadableMap params, Promise promise) {
try {
Expand Down

0 comments on commit 4c8e12c

Please sign in to comment.