reactjs中的登录页面验证

时间:2019-12-07 07:14:28

标签: javascript reactjs react-router

我想将angularjs登录页面转换为reactjs。我做了代码,但我想知道这是否是正确的方法,在我完成之前是否正确。另外,我想添加只能通过身份验证访问的仪表板页面,并且用户登录后将被重定向到仪表板。

$scope.login=function(){
    $scope.newuser={
        'password':$scope.signinpassword,
        'email':$scope.emailid
    }
   return $http.post('/api/authenticate',$scope.newuser).then(function(response,status){
        if(response.data=='redirect'){
            $window.location.href="/dashboard";                        
        }else if(response.data=='verifyemail'){
            angular.element(document.querySelector('#verifyemailbtn')).click();                
        }else if(response.data=='Invalid Password'){
            window.alert("Incorrect Password");
            $scope.error='Failed to authenticate'
        }       
   });
}

到目前为止我翻译的我的reactjs代码

class Login extends Component {
constructor(props){
  super(props);
  this.state={
  emailid:'',
  password:''
  }
 }
performLogin = async (event) => {
    var body={
        "emailid":"this.state.emailid",
        "password":"this.state.password"
        }
    const options = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json"
      },
      body: body
    };
    const url = "api/login";
    try {
      const response = await fetch(url, options);
      const result = await response.json();
      console.log(`login successful`);
    } catch (error) {
      console.error("login credentials wrong");
    }
  };

render() {
    return (
      <div>
             <input type="text"
             placeholder="emailid"
             onChange = {(event,newValue) => this.setState({emailid:newValue})}
             />
            <br/>
             <input
               type="password"
               placeholder="Enter your Password"
               onChange = {(event,newValue) => this.setState({password:newValue})}
              />
            <br/>
             <button type="submit" onClick={(event) => this.performLogin(event)}/>
      </div>
    );
  }
}
export default Login;   

1 个答案:

答案 0 :(得分:1)

您可以接近代码,但是存在一些语法问题。

1-您需要为构造函数中的方法绑定“ this”

2-删除perform.Login中this.state.emailid的双引号

3-您不需要为每个输入提供很多功能,只需使用一个功能即可处理所有输入字段。

我重构您的代码:-)

class Login extends Component {
constructor(props){
  super(props);
  
  this.performLogin = this.performLogin.bind(this)
  this.handleChange = this.handleChange.bind(this)
  this.state={
  emailid:'',
  password:''
  }
 }
 
 
performLogin = async (event) => {
    const { enteredText } = this.state;
    var body={
        "emailid":this.state.emailid,
        "password":this.state.password
        }
    const options = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json"
      },
      body: body
    };
    const url = "api/login";
    try {
      const response = await fetch(url, options);
      const result = await response.json();
      $window.location.href="/dashboard";
    } catch (error) {
      console.error("login credentials wrong");
    }
  };
  
  
  handleChange(e) {
    this.setState({[e.target.name]:e.target.value})
  }

render() {
    return (
      <div>
             <input type="text"
             placeholder="emailid"
             onChange = {handleChange}
             />
            <br/>
             <input
               type="password"
               placeholder="Enter your Password"
               onChange = {handleChange}
              />
            <br/>
             <button type="submit" onClick={this.performLogin}/>
      </div>
    );
  }
}
export default Login;   

您可以使用React-Router之类的lib进行重定向,也可以仅将console.log(login successful)替换为$ window.location.href =“ / dashboard”