简体   繁体   English

React Native,堆栈导航组 Firebase 身份验证保持登录状态

[英]React Native, Stack Navigation Group for Firebase authentication stay logged in

I made a registration, login and home screen integrating firebase on React-Native.我在React-Native上做了一个集成firebase的注册、登录和主屏幕。 They all works fine, and now I'm trying to allow users to stay logged in even if they close the app.它们都工作正常,现在我试图让用户即使关闭应用程序也能保持登录状态。 I do it by using on onAuthStateChanged that actually works and change a variable called isSignedIn, then the navigationKey of the stack group should remove from the stackcontainer the Login and Registration Screens when isSignedIn is equal to 'user' and no more 'guest', but this doesn't happen.我通过使用 onAuthStateChanged 来做到这一点,它实际上有效并更改了一个名为 isSignedIn 的变量,然后当 isSignedIn 等于“用户”并且不再有“访客”时,堆栈组的导航键应该从堆栈容器中删除登录和注册屏幕,但是这不会发生。 This is the code I use:这是我使用的代码:

This updates the isSignedIn correctly这会正确更新 isSignedIn

onAuthStateChanged(authApp, (user) => {
  if (user) {
    isSignedIn = 'user'
    console.log(isSignedIn)
    
  } else {
    isSignedIn = 'guest'
    console.log(isSignedIn)
  }
});

This doesn't works at all这根本不起作用

<NavigationContainer>
      <Stack.Navigator>


          <Stack.Group  navigationKey={isSignedIn ? 'user' : 'guest'}>
          {/* <Stack.Group> */}
            <Stack.Screen name="Login" component={LoginScreen} />
            <Stack.Screen name="Registration" component={RegistrationScreen} />
          </Stack.Group>

          <Stack.Group>
            <Stack.Screen name="Home" component={HomeScreen} options={{headerBackVisible:false}}/>
          </Stack.Group>


      </Stack.Navigator>
    </NavigationContainer>

I have no errors我没有错误

I solved by removing the navigation key and moving the onAuthStateChanged from App.js to a LoadingScreen I created that is now the first stack.screen of the stack.navigator and adding a navigator.navigate('Home'):我通过删除导航键并将 onAuthStateChanged 从 App.js 移动到我创建的 LoadingScreen 来解决,现在它是 stack.navigator 的第一个 stack.screen 并添加了 navigator.navigate('Home'):

In LoadingScreen:在加载屏幕中:

import {  authApp } from '../../config/firebase';
onAuthStateChanged(authApp, (user) => {
    if (user) {
        navigation.navigate('Home')
        
    } else {
        navigation.navigate('Login')
    }
    });

In firebase.js在 firebase.js

import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
  apiKey: "...",
  authDomain: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "...",
  appId: "...",
  measurementId: "...",
};

export const app = initializeApp(firebaseConfig);

export const analytics = getAnalytics(app);

export const authApp = getAuth(app);

Keep in mind that you need to handle the goback button and gesture from going back to the Login screen, and this must be done in two separate ways in android and iOS.请记住,您需要处理返回按钮和返回登录屏幕的手势,这必须在 android 和 iOS 中以两种不同的方式完成。 For android in the Screen you don't want to enable go back gesture(for button see below):对于屏幕中的 android,您不想启用 go 后退手势(按钮见下文):

React.useEffect(() => navigation.addListener('beforeRemove', (e) => {
        // Prevent default behavior of leaving the screen
        if(Platform.OS === "android" && e.data.action.type === "GO_BACK"){
            e.preventDefault();
        }
    }));

For iOS, in the Screen you don't want to enable goback gesture and button(button works for both iOS and android):对于 iOS,在屏幕中您不想启用返回手势和按钮(按钮适用于 iOS 和 android):

<Stack.Screen name="Home" component={HomeScreen} options={{headerBackVisible:false, gestureEnabled: false}}/>

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

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