简体   繁体   English

反应原生 firebase 不保留用户信息

[英]react native firebase not persisting user info

This is a plain vanilla user sign in app.这是一个普通的普通用户登录应用程序。 When a user signs in, they should see the Home screen but for some reason it doesnt get to the Homescreen at all.当用户登录时,他们应该会看到主屏幕,但由于某种原因,它根本没有进入主屏幕。 Full App.js code is below.完整的 App.js 代码如下。

I have a branching condition user?我有一个分支条件用户? goto Home: or just show the login/signup options.转到主页:或仅显示登录/注册选项。 The user variable has a value of 'undefined'.用户变量的值为“未定义”。 Does this meant he value did not persist?这是否意味着他的价值观没有坚持?

import 'react-native-gesture-handler';
import React, { useEffect, useState } from 'react'
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'
import { LoginScreen, HomeScreen, RegistrationScreen } from './src/screens'
import {decode, encode} from 'base-64'
import { firebase } from './src/firebase/config'


if (!global.btoa) {  global.btoa = encode }
if (!global.atob) { global.atob = decode }

const Stack = createStackNavigator();

export default function App() {

  const [loading, setLoading] = useState(true)
  const [user, setUser] = useState(null)
  
  

  useEffect(() => {
    const usersRef = firebase.firestore().collection('users');
    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        console.log(user)
        console.log('user is logged in  ')
        usersRef
          .doc(user.uid)
          .get()  
          .then((document) => {
            const userData = document.data()
            setLoading(false)
            setUser(userData)
          })
          .catch((error) => {
            setLoading(false)
          });
      } else {
        console.log('user is not logged in')
        setLoading(false)
      }
    });
  }, []);
  
  
  console.log(user)

  if (loading) {
    return (
      <></>
    )
  }

  return (  
    <NavigationContainer>
      <Stack.Navigator>
        { user ? (
          <Stack.Screen name="Home">
            {props => <HomeScreen {...props} extraData={user} />}
          </Stack.Screen>
        ) : (
          <>
            <Stack.Screen name="Login" component={LoginScreen} />
            <Stack.Screen name="Registration" component={RegistrationScreen} />
          </>
        )}
      </Stack.Navigator>
    </NavigationContainer>
  );
}

Just to be clear, the first console.log(user) in line no 6 produces a dump of all user variables.为了清楚起见,第 6 行的第一个 console.log(user) 产生了所有用户变量的转储。 The second console.log(user) produces 'undefined'.第二个 console.log(user) 产生“未定义”。

That is the expected behavior.这是预期的行为。

Firebase automatically restores the user credentials from local storage when the app/page reloads. Firebase 在应用程序/页面重新加载时自动从本地存储恢复用户凭据。 But this requires it to call the server, which takes some time and thus happens asynchronously.但这需要它调用服务器,这需要一些时间,因此是异步发生的。 While this is going on your main code continues, and your second log shows (correctly) that the current user is still undefined at this point.虽然这正在进行中,但您的主要代码仍在继续,并且您的第二个日志(正确地)显示当前用户此时仍未undefined

Then, when the user credentials are restored, your onAuthStateChanged callback is executed, it sets user and correctly logs the user's properties.然后,当恢复用户凭据时,将执行您的onAuthStateChanged回调,它设置user并正确记录用户的属性。 And since you call setUser(userData) in there, this will also trigger React to re-render the component, so that it can show the updated user in <HomeScreen {...props} extraData={user} /> .并且由于您在那里调用setUser(userData) ,这也会触发 React 重新渲染组件,以便它可以在<HomeScreen {...props} extraData={user} />中显示更新的用户。

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

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