我正致力于在反应本地的仪器方面进行认证,当时,在我的一个用户记录中,我拯救了原体,然后使用标语显示经认证和未经验证的屏幕。 当我日志时,被点击器得到成功救治,但照像没有显示需要认证的屏幕,除非我重新装载或更新被打成像的屏幕,然后再把需要认证的屏幕带上。
因此,我试图通过创建功能标志来迫使更新,并将它纳入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;