代码之家  ›  专栏  ›  技术社区  ›  joshkmartinez

在react native中调用身份验证函数

  •  0
  • joshkmartinez  · 技术社区  · 8 年前

    我正在为我的应用程序设置一个基本的身份验证屏幕。
    我现在要做的是一个按钮调用一个函数 auth
    我的问题是,当我点击按钮,然后调用函数时,函数中似乎什么都没有执行。
    这是我的代码:

    import React from "react";
    import {
      View,
      TextInput,
      StyleSheet,
      Alert,
      KeyboardAvoidingView,
      Dimensions
    } from "react-native";
    import { Button, Text } from "native-base";
    
    
    let width = Dimensions.get("window").width;
    let height = Dimensions.get("window").height;
    
    export default class Login extends React.Component {
      static navigationOptions = {
        title: "Login to Aeries"
      };
    
      constructor(props) {
        super(props);
        this.state = { isAuthed: false, username: "", authError: false, password: "" };
        this.auth = this.auth.bind(this);
    
    
        const showAlert = () => {
          Alert.alert(
            'There was an error loging in. Please check your username and password and try again.'
          )
        }
      }
    
      auth(){
    
        //do auth 
        this.setState({ authError: false,isAuthed: false });
    
        if (this.state.isAuthed === false && this.state.isAuthed === false && this.state.username == "admin" && this.state.password == "admin") { // authentication is successful
          this.setState({ isAuthed: true });
          this.props.navigation.navigate("Grades")
        } else { // auth error
          this.setState({ authError: true,isAuthed: false });
          this.showAlert
        }
    
      };
      render() {
        const { showAlert } = this.state
        return (
          <View style={styles.wrapper}>
            <KeyboardAvoidingView behavior="padding" style={styles.loginContainer}>
              <TextInput
                placeholder="school email"
                keyboardType="email-address"
                autoCapitalize="none"
                autoCorrect={false}
                placeholderTextColor="white"
                style={styles.input}
                value={this.state.username}
              />
    
              <TextInput
                placeholder="password"
                secureTextEntry
                autoCorrect={false}
                autoCapitalize="none"
                placeholderTextColor="white"
                style={styles.input}
                value={this.state.password}
              />
    
              <View style={{
                flexDirection: 'column',
                justifyContent: 'center',
                alignItems: 'center',
                margin: 12
              }}>
    
                <Button
                  primary
                  onPress={this.auth}
                >
    
                  <Text> Login </Text>
    
                </Button>
              </View>
    
            </KeyboardAvoidingView>
    
          </View>
        );
      }
    }
    
    const styles = {
      loginContainer: {
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
        width: width - 42,
        backgroundColor: "#bdc3c7",
        borderRadius: 9,
        marginTop: 21,
        marginBottom: 21
      },
    
      input: {
        paddingHorizontal: 10,
        color: "#2c3e50",
        backgroundColor: "#95a5a6",
        margin: 12,
        height: 42,
        width: width - 69,
        borderRadius: 3
      },
    
      logo: {
        width: 231,
        height: 231
      },
    
      wrapper: {
        flex: 1,
        alignItems: "center",
        justifyContent: "center",
        marginTop: -21
      },
    
      loginButton: {
        borderRadius: 3,
        marginTop: 9,
        marginBottom: 21
      },
      error: {
        color: 'red',
        textAlign: 'center',
      }
    };
    

    当我点击按钮时应该发生的是,它应该调用 认证 作用

    auth函数应该检查用户名和密码是否都正确 admin 如果是这样,请导航到名为 Grades (顺便说一句,我正在使用react导航)

    如果用户名和密码不是“admin”,则会显示一个警报,说明身份验证失败。

    提前谢谢。如果您想让我附加任何其他代码,请告诉我。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Rajendran Nadar    8 年前

    你的方法是错误的,这样做吧

    在构造函数中绑定函数

    constructor(props) {
       super(props);
       this.auth = this.auth.bind(this);
    }
    

    这样设置函数

    auth() {
       // Your chunk of code
    }
    

    按钮应该是这样的

    <Button onPress={this.auth} />
    

    如果有任何问题,请在评论部分告诉我!

    看看这个 live demo

    推荐文章