1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

React NativeでFirebase authのメール検証値の変更を監視する方法

Posted at

Firebase authで変更監視できるものは下記のみで、メール検証値は自分で監視するしかない

  • onAuthStateChanged
    • サインイン、サインアウトしたときに通知される
  • onIdTokenChanged
    • トークンが変更さたときに通知される
  • beforeAuthStateChanged
    • 新しいユーザが設定される前に通知される(使用していないのでよくわからない)

メール検証値を監視するソース

  const user = getAuth().currentUser;
  const [emailVerified, setEmailVerified] = useState(user?.emailVerified);
  let timeoutId : any;

  useEffect(() => {
    if (emailVerified) return;
    const onIdTokenChangedUnsubscribe = getAuth().onIdTokenChanged((user) => {
      if (timeoutId) {
        clearTimeout(timeoutId);
      }
      if (user && user.emailVerified) {
        console.log("verified");
        setEmailVerified(user.emailVerified);
        return onIdTokenChangedUnsubscribe();
      }
      timeoutId = setTimeout(() => {
        console.log("unverified");
        getAuth().currentUser?.reload();
      }, 10000);
    });
  }, []);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?