如何 return 一个布尔值或只是一个在身份验证成功时通知的承诺?

How to return a Boolean or just a promise that notifies when authentication succeeded?

我已经使用 react-native-fingerprint-scannerReact Native 中实现了指纹验证代码,并且工作正常。我的担心听起来可能很愚蠢,但实际上当身份验证成功时我仍然坚持这一点我不知道如何检查它以便我可以在那里触发 setState。此代码仅返回一个字符串:

FingerprintScanner
        .authenticate((description, onAttempt) => {
            if (description) {
                this.setState({ Authenticated: true })
            }
            //description: 'Log in with Biometrics'
        })
        .then(() => {
            this.props.onAuthenticate();
        })

注意:执行永远不会到达 .then() 回调,否则我会在那里设置状态。不知道为什么会这样!

FingerprintScanner
  .authenticate({ description: 'Scan your fingerprint on the device scanner to continue' })
  .then(() => {
    //put your set state here
    Alert.alert('Authenticated successfully');
  })
  .catch((error) => {
    Alert.alert(error.message);
  });
}

希望对你有所帮助

我猜你读得 the docs example 不是很好。注意以下代码:

componentDidMount() {
  FingerprintScanner
    .authenticate({ // calling the fingerprint authentication pop-up and it waits for your acts
      description: 'the description for your user that what does he/she to do',
    })
    .then(() => { // successful callback
      this.props.handlePopupDismissed(); // close the auth pop-up
      // here, do what you want after successful authentication passing
    })
    .catch((error) => { // failure callback (fallback)
      this.props.handlePopupDismissed(); // close the auth pop-up
      // here, do what you want after failing the authentication by user
    });
}

上面代码里面的注释说明的很清楚

在调用Authenticate之前或之后,我需要调用这个方法来释放缓存:

FingerprintScanner.release();

这样用户可以随时取消并重试。