【问题标题】:Face Id Authentication for android in React-NativeReact-Native 中 android 的 Face Id 身份验证
【发布时间】:2020-09-09 07:01:25
【问题描述】:

我想要 android face_id 身份验证 sdk 用于我的 react 本机 android 应用登录。我使用了仅支持安卓指纹认证和密码的插件。请让我知道是否有任何可用的插件或 sdk,以便我可以在 react native android 中使用。

如何通过创建本机模块在本机反应中使用此代码。 https://developer.android.com/training/sign-in/biometric-auth

private Executor executor;
private BiometricPrompt biometricPrompt;
private BiometricPrompt.PromptInfo promptInfo;

executor = ContextCompat.getMainExecutor(this);
biometricPrompt = new BiometricPrompt(MainActivity.this,
        executor, new BiometricPrompt.AuthenticationCallback() {
    @Override
    public void onAuthenticationError(int errorCode,
            @NonNull CharSequence errString) {
        super.onAuthenticationError(errorCode, errString);
        Toast.makeText(getApplicationContext(),
            "Authentication error: " + errString, Toast.LENGTH_SHORT)
            .show();
    }

    @Override
    public void onAuthenticationSucceeded(
            @NonNull BiometricPrompt.AuthenticationResult result) {
        super.onAuthenticationSucceeded(result);
        Toast.makeText(getApplicationContext(),
            "Authentication succeeded!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAuthenticationFailed() {
        super.onAuthenticationFailed();
        Toast.makeText(getApplicationContext(), "Authentication failed",
            Toast.LENGTH_SHORT)
            .show();
    }
});

promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .build();

【问题讨论】:

    标签: android react-native face-id


    【解决方案1】:

    在 Android 中,最新设备通常使用指纹和 iOS Face ID,我认为在 Android 中无法以类似的方式管理 Face ID。

    例如,library 表示 Face-ID 和 Touch-ID 仅适用于 iOS 设备,而 Biometrics 适用于 Android,其工作方式与 Touch-ID 类似。

    其他用户回答了类似的question

    【讨论】:

      【解决方案2】:

      这是您使用的众多 api 之一react-native-fingerprint-scanner 和下面的示例:

      const isBiometricValid = async () =>
        FingerprintScanner.authenticate({
          description: 'Scan your fingerprint on the device scanner to continue'
        });
      
      const availableBiometric = async () => FingerprintScanner.isSensorAvailable();
      
      const secureLogin = async ({
        cpf,
        password,
        setFieldError,
        storedUser,
        navigation,
        dispatch,
        setUseManualPassword,
        useManualPassword,
        setFieldValue
      }) => {
        const authArgs = {
          cpf,
          password,
          setFieldError,
          navigation,
          dispatch
        };
      
        try {
          const availableBiometricType = await availableBiometric();
      
          if (!availableBiometricType) {
            return AuthCreators.getToken({ ...authArgs });
          }
      
          if (useManualPassword) {
            return AuthCreators.getToken({
              ...authArgs,
              cpf: storedUser[0]
            });
          }
      
          if (storedUser) {
            const userAccessGranted = await isBiometricValid();
      
            if (userAccessGranted) {
              const storedPasswordEncrypted = await AsyncStorage.getItem('@__:password');
              const decryptedPasswordBytes = CryptoJS.AES.decrypt(
                storedPasswordEncrypted,
                Config.USER_PASSWORD_ENCRYPTION_KEY
              );
              const decryptedPassword = decryptedPasswordBytes.toString(CryptoJS.enc.Utf8);
      
              return AuthCreators.getToken({
                ...authArgs,
                cpf: storedUser[0],
                password: decryptedPassword
              });
            }
          }
      
          return AuthCreators.getToken({
            ...authArgs,
            biometric: normalizedBiometricType(availableBiometricType)
          });
        } catch (error) {
          return biometricAuthFailure({
            ...authArgs,
            error,
            setUseManualPassword,
            setFieldValue,
            storedUser
          });
        }
      };
      

      您可以通过打开 iOS 11 等设备(或任何默认使用 faceID 的设备)进行测试和调试,然后在 Features > FaceID > Enrolled 中注册生物识别。

      【讨论】:

      • 我用过你提到的插件,但是face id只适用于ios而不适用于android。
      • 可能您尝试使用 faceId 的设备没有此模式可用...
      猜你喜欢
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2017-07-12
      • 2016-12-25
      • 1970-01-01
      相关资源
      最近更新 更多