English 中文(简体)
2. 在原木后向Austh 屏幕进行不再活的原住民
原标题:React Native not Navigating to Auth screen after login

我正致力于在反应本地的仪器方面进行认证,当时,在我的一个用户记录中,我拯救了原体,然后使用标语显示经认证和未经验证的屏幕。 当我日志时,被点击器得到成功救治,但照像没有显示需要认证的屏幕,除非我重新装载或更新被打成像的屏幕,然后再把需要认证的屏幕带上。

因此,我试图通过创建功能标志来迫使更新,并将它纳入Login功能,以了解Aync储存所节省的标记是否将显示,但并未奏效,我也感到困惑,即:在航道里,在用户标识之后,是否出现,而标语甚至在我更新或重新开口之后根本就没有显示。

// authUtils.ts
import { useEffect, useState } from "react";
import { getUserToken, getAdminToken } from "./localService";

export const useAuth = () => {
  const [isLoading, setIsLoading] = useState(true);
  const [userToken, setUserToken] = useState(null);
  const [adminToken, setAdminToken] = useState(null);

  const fetchData = async () => {
    try {
        console.log("fetch data triggeredssss")

      const token = await getAdminToken();
      if (token) {
        setAdminToken(token);
      } else {
        const userToken = await getUserToken();
        setUserToken(userToken);
      }
    } catch (error) {
      console.log("Error while fetching data:", error);
    } finally {
      setIsLoading(false);
    }
  };

  useEffect(() => {
    fetchData();
  }, []);

  return { isLoading, userToken, adminToken };
};

localService.ts:

export const saveAdminToken = async (token: any) => {
  try {
    if (token) {
      await AsyncStorage.setItem("admin", token);
    } else {
      console.log("Error saving token: Invalid token");
    }
  } catch (e) {
    console.log("Error saving token:", e);
  }
};

export const saveUserToken = async (token: any) => {
  try {
    if (token) {
      await AsyncStorage.setItem("user", token);
    } else {
      console.log("Error saving token: Invalid token");
    }
  } catch (e) {
    console.log("Error saving token:", e);
  }
};

export const getAdminToken = async () => {
  try {
    const token = await AsyncStorage.getItem("admin");
    return token;
  } catch (error) {
    console.log("Error while retrieving admin token:", error);
    return null;
  }
};

export const getUserToken = async () => {
  if (typeof window !== "undefined") {
    const token = await AsyncStorage.getItem("user");
    if (token) {
      return token;
    }
    return null;
  }
};

router.tsx:

import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import Onboarding from "../screens/onboarding";
import AdminHomeStack from "./AdminHomeStack";
import UserHomeTabs from "./UserHomeTabs";
import UserSignup from "../screens/user/signup";
import UserLogin from "../screens/user/login";
import AdminSignup from "../screens/admin/signup";
import AdminLogin from "../screens/admin/login";
import { useAuth } from "../../services/authUtils";
import { ActivityIndicator } from "react-native";

const Stack = createNativeStackNavigator();

const Router = () => {
  const { isLoading, userToken, adminToken } = useAuth();
  if (isLoading) {
    return <ActivityIndicator />;
  }
  console.log(`admin ${adminToken}`);
  return (
    <NavigationContainer>
      {userToken ? (
        <UserStack />
      ) : adminToken ? (
        <AdminHomeStack />
      ) : (
        <Stack.Navigator screenOptions={{ headerShown: false }}>
          <Stack.Screen name="Onboarding" component={Onboarding} />
         
          <Stack.Screen name="AdminLogin" component={AdminLogin} />
          <Stack.Screen name="AdminSignup" component={AdminSignup} />

          <Stack.Screen name="UserSignup" component={UserSignup} />
          <Stack.Screen name="UserLogin" component={UserLogin} />
        </Stack.Navigator>
      )}
    </NavigationContainer>
  );
};

const UserStack = () => {
  return (
    <Stack.Navigator screenOptions={{ headerShown: false }}>
      <Stack.Screen name="User" component={UserHomeTabs} />
    </Stack.Navigator>
  );
};

export default Router;

问题回答

让我们设法在圣克顶上填上<代码><Stack.Navigator>。

return <NavigationContainer>
      <Stack.Navigator>
        {authState != null ? (
            LoginedStack()
        ) : (
            <Stack.Screen name= LoginScreen 
                component={LoginScreen}
                options={{
                    presentation:  fullScreenModal ,
                    headerShown: false,
                    unmountInactiveRoutes: true
                }} />
        )}
    </Stack.Navigator>
  </NavigationContainer>





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签