简体   繁体   English

在 React-Native 中登录/退出 Firebase 的最佳方式?

[英]Best way to sign in/out from Firebase in React-Native?

I am having a problem understanding how to implement:我在理解如何实施时遇到问题:

  1. sign a user in让用户登录
  2. keep listening to the user changes不断倾听用户的变化
  3. sign a user out注销用户
  4. unsubscribe to the listener取消订阅监听器

using :使用 :

const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
    if (user) {
        // Signed in
    } else {
        // Signed out
    }
});
unsubscribe();

I understand that the above listener is recommended but I don't understand how to use it.我知道建议使用上述侦听器,但我不明白如何使用它。 Can someone explain in more details?有人可以更详细地解释一下吗?

I wanna know if this function should be called when I first sign a user in, but then when and how should I sign them out?我想知道当我第一次登录用户时是否应该调用这个函数,但是我应该何时以及如何将它们注销? Should I call this function in each screen of the app (for ex. in componentDidMount)?我应该在应用程序的每个屏幕中调用这个函数吗(例如在 componentDidMount 中)?

Moreover, where should I unsubscribe() to the listener?此外,我应该在哪里unsubscribe()到侦听器? In which screen?在哪个屏幕? Let's say there are THREE screens: Login, Screen 1, Screen 2 .假设有三个屏幕:登录、屏幕 1、屏幕 2 I call the listener in Login screen, then when the user is in Screen 1 or Screen 2 , how am I supposed to keep listening to the changes and sign him out when I should AND unsubscribe to the listener?我在登录屏幕中调用监听器,然后当用户在屏幕 1 或屏幕 2 中时,我应该如何继续监听更改并在我应该取消订阅监听器时让他退出?

firebase.auth().onAuthStateChanged opens up a listener, so a good idea is to make a useEffect that has the user as input, and change the user state according to if you are signed in or not. firebase.auth().onAuthStateChanged 打开一个监听器,所以一个好主意是制作一个以用户为输入的 useEffect,并根据您是否登录来更改用户状态。 Once you use the sign out method, the onAuthStateChange will trigger.使用注销方法后,将触发 onAuthStateChange。 Then you can pass down the user everywhere through props o any global state you like.然后你可以通过 props 或任何你喜欢的全局状态在任何地方传递用户。

 useEffect(() => {
    firebase.auth().onAuthStateChanged((user) => {
      if (user) {
         setUser(user)
      } else {
          setUser(null)
      }
  });
   
  }, [user])

I use react-query instead of useEffect, and then I get the user claims, just to give you another example.我使用react-query而不是useEffect,然后我得到了用户声明,只是再举个例子。

 const fetchClaims = async () => {
    setLoading(true);

    const fetchedUser = await auth.onAuthStateChanged((user) => {
      if (user) {
        user.getIdTokenResult().then((idTokenResult) => {
          // console.log(idTokenResult.claims);
          setIsClient(idTokenResult.claims.client);
          setIsMaster(idTokenResult.claims.master);
          setNickName(idTokenResult.claims.name);
          setUserEmail(idTokenResult.claims.email);
          setClientName(idTokenResult.claims.clientName);
          setIsPremium(idTokenResult.claims.premium);
          setUserPicture(idTokenResult.claims.picture);
        });
        setUser(user);
        setLoading(false);
      } else {
        setUser(null);
        setIsClient(false);
        setIsMaster(false);
        setClientName(null);
        setUserEmail(null);
        setLoading(false);
        setIsPremium(false);
      }
    });
    return fetchedUser;
  };




  useQuery("fetchClaimsData", fetchClaims );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM