【问题标题】:How to reset recaptcha when using react-redux-firebase使用 react-redux-firebase 时如何重置验证码
【发布时间】:2020-10-22 01:25:09
【问题描述】:

我正在使用 React-Redux-Firebase。我实现了使用电话号码登录。现在我正在尝试实现错误处理。当数字无效时,我会显示带有错误消息的窗口警报。剩下要做的就是reset recaptcha。没有它,我会收到错误:

reCAPTCHA 已在此元素中呈现

我正在尝试根据 Firebase 文档进行操作

grecaptcha.reset(window.recaptchaWidgetId);

// 或者,如果您还没有存储小部件 ID:

window.recaptchaVerifier.render().then(function(widgetId) { grecaptcha.reset(widgetId); }

但它在我的代码中不起作用。我没有实现grecaptcha。我尝试用react-grecaptcha添加它,但它不起作用。

有人可以给我一个提示如何在每次错误后重置验证码,好吗?

  state = {
    phone: "",
    confirmationResult: {},
  };

  handleClick = () => {
    const recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
      "sign-in-button",
      {
        size: "invisible",
      }
    );
    firebase
      .signInWithPhoneNumber(`+${this.state.phone}`, recaptchaVerifier)
      .then((confirmationResult) => {
        this.setState({ confirmationResult });
      })
      .catch((error) => {
        // Error; SMS not sent
        // Handle Errors Here
        window.alert(`${error.code}, ${error.message}`);
        recaptchaVerifier.reset(); // How can I do that?
      });
  };

【问题讨论】:

  • 嗨,欢迎来到 SO!我不是 FIrebase 专家,但您不是在寻找 .clear() 方法吗? firebase.google.com/docs/reference/js/…
  • 您好,感谢您的回复。当我尝试.clear() 时,我收到错误对象:code: "auth/internal-error" message: "RecaptchaVerifier instance has been destroyed"

标签: reactjs firebase-authentication recaptcha invisible-recaptcha react-redux-firebase


【解决方案1】:

我不是专家,但根据文档以及在评论部分与您交谈,我认为您需要通过 callback。像这样:

const recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
  'size': 'invisible',
  'callback': function(response) {
    // reCAPTCHA solved, allow signInWithPhoneNumber.
   firebase
  .signInWithPhoneNumber(`+${this.state.phone}`, recaptchaVerifier)
  .then((confirmationResult) => {
    this.setState({ confirmationResult });
  })
  .catch((error) => {
    // Error; SMS not sent
    // Handle Errors Here
    window.alert(`${error.code}, ${error.message}`);
    recaptchaVerifier.reset();
  });
  }
});

参考:https://firebase.google.com/docs/auth/web/phone-auth#use-invisible-recaptcha

希望这会有所帮助!

【讨论】:

  • 谢谢您,您的帮助。我的问题的原因是我没有处理 recaptcha 的回调。我也没有完全理解文档,那里有解释。
猜你喜欢
  • 2019-03-25
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2017-05-10
  • 1970-01-01
  • 2020-03-11
  • 2018-09-25
相关资源
最近更新 更多