Firebase React Native – 用户不会自动更新

huangapple go评论74阅读模式
英文:

Firebase React Native - User doesn't update automatically

问题

我正在使用Firebase制作电子邮件验证系统。
基本上,我创建了一个页面,用于注册用户,然后登录用户。

我获取用户如下:

import { useAuthState } from "react-firebase-hooks/auth";

const [user, loading, error] = useAuthState(FirebaseAuth)

在下一个页面上,我要求用户检查他的电子邮件,因为我发送了一封确认邮件。

我使用了一个带有[user]作为依赖项的useEffect钩子,在其中使用console.log("test")。
除了在页面加载时,useEffect从未被调用过。

然而,在后台,用户的电子邮件已经被验证,所以应该执行useEffect,因为user.emailVerified已更改。

我还尝试使用onAuthStateChanged,但是即使在电子邮件被验证后,用户仍然保持为"null"...

const [user, setUser] = useState(null);

useEffect(() => {
  const auth = getAuth(firebase_app);

  const unsubscribe = onAuthStateChanged(auth, (authUser) => {
    if (authUser) {
      setUser(authUser);
    }
  });

  return () => unsubscribe();
}, []);
英文:

I'm doing an email verification system using Firebase.
So basically, I made a page which sign up, then sign in a user.

I get the user like so :

import {useAuthState} from "react-firebase-hooks/auth";

const [user, loading, error] = useAuthState(FirebaseAuth)

On the next page I ask the user to check his mails, because I sent a confirmation mail.

I'm using a useEffect hook, with [user] as dependency, where I console.log("test").
The useEffect is never called except at the mount of the page.

And yet, in the background, the email of the user is verified, so useEffect should get executed since user.emailVerified changed.

I also tried to use onAuthStateChanged, but user was remaining to "null", even after the email got verified.... :

  const [user, setUser] = useState(null);

  useEffect(() => {
    const auth = getAuth(firebase_app);

    const unsubscribe = onAuthStateChanged(auth, (authUser) => {
      if (authUser) {
        setUser(authUser);
      }
    });


    return () => unsubscribe();
  }, []);

答案1

得分: 1

这似乎是一个“上下文”问题,你的逻辑仍在使用“旧”的上下文/引用。

以下是我过去如何处理电子邮件确认的方式:

  let emailInterval = useRef<NodeJS.Timer>();
  useEffect(() => {
    emailInterval.current = setInterval(async () => {
      // 确保在用户确认电子邮件后,我们拥有最新版本的当前用户
      await auth().currentUser?.reload();

      // 检查用户是否已确认电子邮件
      if (auth().currentUser?.emailVerified) {
        handleEmailVerified();
      } else {
        if (!emailConfirmationHasBeenSent) {
          sendEmailConfirmation();
        }
      }
    }, 5000);

    return () => clearEmailInterval();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const clearEmailInterval = () => {
    if (emailInterval.current) {
      clearInterval(emailInterval.current);
    }
  };
英文:

I'm not 100% sure but I think this a 'context' issue where your logic is still using an 'old' context/reference.

Here's how I've managed the email confirmation in the past:

  let emailInterval = useRef&lt;NodeJS.Timer&gt;();
  useEffect(() =&gt; {
    emailInterval.current = setInterval(async () =&gt; {
      // Make sure we have the most recent version of the current user after
      // they&#39;ve confirmed their email
      await auth().currentUser?.reload();

      // Check if the user had confirmed their email
      if (auth().currentUser?.emailVerified) {
        handleEmailVerified();
      } else {
        if (!emailConfirmationHasBeenSent) {
          sendEmailConfirmation();
        }
      }
    }, 5000);

    return () =&gt; clearEmailInterval();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  const clearEmailInterval = () =&gt; {
    if (emailInterval.current) {
      clearInterval(emailInterval.current);
    }
  };

答案2

得分: 1

Email verification happens out-of-band, as it happens in the users email client or in their browser, and not in your application. This means that your application won't know about the change in verification state, until it refreshes the ID token of the users, which is does:

  • When the user signs-in again.
  • Automatically once every hour, a few minutes before the current ID token expires.
  • When you force it to refresh the ID token by calling reload() or getIdToken(true) on the user.

This has been covered before, so also have a look at:

英文:

Email verification happens out-of-band, as it happens in the users email client or in their browser, and not in your application. This means that your application won't know about the change in verification state, until it refreshes the ID token of the users, which is does:

  • When the user signs-in again.
  • Automatically once every hour, a few minutes before the current ID token expires.
  • When you force it to refresh the ID token by calling reload() or getIdToken(true) on the user.

This has been covered before, so also have a look at:

huangapple
  • 本文由 发表于 2023年6月26日 06:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定