在React Native中使用wordpress JWT登录身份验证时遇到问题

时间:2020-03-23 09:37:33

标签: react-native wordpress-rest-api jwt-auth

我想使用Wodpress JWT令牌在React Native中进行登录身份验证。我也已经完成了获取用户数据的所有操作,但是单击登录按钮后,我不知道如何导航到用户屏幕。请查看我的代码,并帮助我。谢谢

嗨,我想用Wodpress JWT令牌在React Native中进行登录身份验证。我也已经完成了获取用户数据的所有操作,但是单击登录按钮后,我不知道如何导航到用户屏幕。请查看我的代码,并帮助我。谢谢

class SetupLogin extends Component {
 this.state = {
          username: '',
          password: '',
          userNiceName: '',
          userEmail: '',
          loggedIn: false,
          loading: false,
          error: '',
        };
      }
onFormSubmit = event => {
event.preventDefault();
const url = 'https://staging.islamicmedia.com.au/';
const loginData = {
  username: this.state.username,

  password: this.state.password,
};

this.setState({laoding: true}, () => {
  axios
    .post(`${url}wp-json/jwt-auth/v1/token`, loginData)
    .then(res => {
      console.log(res.data);
      if (undefined === res.data.token) {
        this.setState({error: data.message, loading: false});
        return;
      }

      localStorage.setItem('token', res.data.token);
      localStorage.setItem('userName', res.data.user_nicename);
      localStorage.setItem('userId', res.data.user_id);
      this.setState({
        loading: false,
        token: res.data.token,
        userNiceName: res.data.user_nicename,
        userEmail: res.data.user_email,
        loggedIn: true,
      });
    })
    .catch(error => {
      this.setState({
        error: error.response.data,
        loading: false,
      });
    });
})}




 handleChange = text => {
    this.setState({
      username: text,
    });
  };

  render() {
    const {username, loggedIn, password} = this.state;

返回声明

<View style={styles.inputContainer}>
      <TextInput
        value={username}
        onChangeText={this.handleChange}
        placeholder="Name"
        underlineColorAndroid="transparent"
        style={styles.textInput}></TextInput>
    </View>
    <View style={styles.inputContainer}>
      <TextInput
        placeholder="Email"
        underlineColorAndroid="transparent"
        style={styles.textInput}></TextInput>
    </View>
    <View style={styles.inputContainer}>
      <TextInput
        value={password}
        onChangeText={pass => this.setState({password: pass})}
        placeholder="Password"
        underlineColorAndroid="transparent"
        secureTextEntry={true}
        style={styles.textInput}></TextInput>
    </View>

    <View style={{flexDirection: 'row'}}>
      <Button style={styles.signUp}>
        <Text style={styles.signUpText}>Sign Up</Text>
      </Button>
      <Button
        onPress={this.onFormSubmit}
        // onPress={() => this.props.navigation.navigate('Mainhome')}
        style={styles.login}>
        <Text style={styles.loginText}>LOG IN</Text>
      </Button>
    </View>

0 个答案:

没有答案