身份认证
Route.js 路由页面
import { createSwitchNavigator } from "react-navigation";
import Auth from "./Auth";
import Home from "./Home";
import Login from "./Login";
const Route = createSwitchNavigator(
{
Auth: Auth,
Home: Home,
Login: Login
},
{
initialRouteName: "Auth"
}
);
Auth.,js 认证页面(状态判断)
//判断登录认证状态:true:Home false:Login
_bootstrapAsync = async () => {
//获取Sp文件中保存的用户信息,用户判断用户登录的状态
const name = await AsyncStorage.getItem("name");
if (name && name.length >= 6) {
this.props.navigation.navigate("Home");
} else {
this.props.navigation.navigate("Login");
}
// const userToken = await AsyncStorage.getItem("userToken");
// this.props.navigation.navigate(userToken ? "App" : "Auth");
};
Login.js 登录页面(用户登录)
_signInAsync = async () => {
//保存登录信息到SP文件
await AsyncStorage.setItem("name", "abc123456");
//登录后跳转到主页面
this.props.navigation.navigate("Home");
};
Home.js 主页面 (注销功能)
_signOutAsync = async () => {
//将Sp文件中保存的用户信息清空
await AsyncStorage.clear();
//跳转到登录页面
this.props.navigation.navigate("Login");
};
网友评论