【问题标题】:How to prevent dynamicLink listener from firing multiple times in react-native?如何防止dynamicLink监听器在react-native中多次触发?
【发布时间】:2020-04-27 20:32:00
【问题描述】:

我已在我的 react-native 应用中从 firebase 启用 signInWithEmailLink()

一切正常,用户也已创建,但我认为onLink(handleLink) 侦听器被多次触发,即使在用户登录后也会导致错误。

日志:

link is tru
 LOG  email:  email@gmail.com //I have hidden the actual email
 LOG  email:  email@gmail.com
 LOG  email:  email@gmail.com
 LOG  USer created
 LOG  EROR:  [Error: [auth/invalid-action-code] The out of band code is invalid. This can happen if the code is malformed, expired, or has already been used.]
 LOG  EROR:  [Error: [auth/invalid-action-code] The out of band code is invalid. This can happen if the code is malformed, expired, or has already been used.]
 LOG  EROR:  [Error: [auth/invalid-action-code] The out of band code is invalid. This can happen if the code is malformed, expired, or has already been used.]

您可以看到它多次触发,我该如何防止这种情况发生??

这是我的代码:

const handleLink = async link => {
  console.log('opened Link: ', link.url);
  if (auth().isSignInWithEmailLink(link.url)) {
    console.log('link is tru');
    try {
      const email = await AsyncStorage.getItem('email');
      console.log('email: ', email);
      await auth()
        .signInWithEmailLink(`${email}`, link.url)
        .then(() => {
          console.log('USer created');
        })
        .catch(err => {
          console.log('EROR: ', err);
        });
    } catch (err) {
      console.log('err: ', err);
    }
  } else {
    console.log('link is false');
  }
};

const link = dynamicLinks().onLink(handleLink);

非常感谢您的帮助

【问题讨论】:

    标签: react-native firebase-authentication firebase-dynamic-links react-native-firebase


    【解决方案1】:

    由于console.log('email: ', email);被调用了3次,我假设console.log('link is tru');也被调用了3次,所以问题是整个handleLink函数在auth().signInWithEmailLink完成之前被调用了3次。

    【讨论】:

    • 是的,你是对的,但我应该如何防止这种情况发生?
    • 我猜你打电话给dynamicLinks().onLink(handleLink) 的地方是错误的。尝试将它放在您的 App 组件的 componentDidMount 中,以便它只执行一次
    【解决方案2】:

    我遇到了同样的问题,这里就是答案。

    Email link Authentication has a longer expiration time (around 6 hours). If you'll create 2 or more sign in emails in a row, you should be able to login with the latest link being received.You'll also be able to receive an error message for example in iOS, "The action code is invalid.This can happen if the code is malformed, expired, or has already been used" when you clicked the older links.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-09
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2017-12-03
      • 2017-12-07
      • 1970-01-01
      相关资源
      最近更新 更多