RN身份认证
**
**
RN身份认证
你好!今天学习一下身份认证
首先Route.js
import { createSwitchNavigator } from "react-navigation";
const Route = createSwitchNavigator({
AuthLoading: { screen: AuthLoadingScreen },
Home: { screen: HomeScreen },
SignIn: { screen: SignInScreen }
});
导入: AsyncStorage
import { Platform, StyleSheet, Text, View, TextInput,
Button, AsyncStorage } from “react-native”;
AuthLoadingScreen.js 判断登录状态,跳转不同页面
//判断SP文件中是否保存了userToken字段:true 登录状态 false:未登录状态(读SP文件状态)
_bootstrapAsync = async () => {
//获取登录状态
const userToken = await AsyncStorage.getItem(“userToken”);
//根据状态判断:跳转不同页面
this.props.navigation.navigate(userToken ? “Home” : “SignIn”); };
登陆界面:
constructor(props) {
super(props);
this.state = {
id: “”
}; }DengLu = async () => {
await AsyncStorage.setItem(“id”, this.state.id);//登录,设置登录数据
//跳转到主页面
this.props.navigation.navigate(“Home”);
};
render() {
return (
<TextInput
onChangeText={id => {
this.setState({ id });
}}
/>
); } }
HomeScreen.js 进入主页面,说明已经登录成功了,用户有可能注销登录(将SP文件中的登录信息清空)
<Button
title={“注销”}
onPress={async () => {
//注销清空数据
await AsyncStorage.clear();
//跳转到登录页面
this.props.navigation.navigate(“SignIn”);
}}
/>
下一篇: 使用Redux工具调试