簡體   English   中英

如何返回 Boolean 或僅在身份驗證成功時通知的 promise?

[英]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 .then()回調,否則我只會在那里設置 state 。 不知道為什么會這樣!

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);
  });
}

我希望這會有所幫助

我猜你沒有很好地閱讀文檔示例 注意以下代碼:

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();

這樣用戶可以隨時取消並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM