简体   繁体   English

Node.js React JWT登录

[英]Node.js React JWT Login

It's ok, I got my Token on Node.js, I tested with Postman. 没关系,我在Node.js上获得了令牌,并与Postman进行了测试。 But I want to recuperate my Token in React with localStorage. 但是我想在React中使用localStorage恢复我的令牌。 I don't understand why, but it doesn't work. 我不明白为什么,但是它不起作用。 Browser toolbox says "TypeError: this is undefined". 浏览器工具箱显示“ TypeError:这是未定义的”。

When I try with "bind", browser say nothing. 当我尝试使用“绑定”时,浏览器什么也不说。 No error but my localStorage is empty. 没有错误,但是我的localStorage为空。 For the second case, PreventDefault() doesn't work, i replaced it by persist(). 对于第二种情况,PreventDefault()不起作用,我将其替换为persist()。

Login.js Login.js

class Login extends Component {

  state = {
    email:"",
    password:"",
  }

async postLogin(e){

  await this.setState({
    [e.target.name]: e.target.value
  })

    e.preventDefault(e);
    const login = {
        email:this.state.email,
        password:this.state.password
    };

  await axios.post("http://localhost:7777/api/users/login", login)
        .then((response) => {
            const {token} = response.data;
            localStorage.setItem("token", token)
            this.props.history.push("/articles")
        })

        .catch((error) => {
            console.log(error);
        })
}


  render(){

    return(
        <div className="containerSign">
          <h1>Connecte toi</h1>
          <form onSubmit={this.postLogin}>
            <input className="inputFormSign" type="email" placeholder="Email"/>
            <input className="inputFormSign" type="password" placeholder="Mot de passe"/>
            <button className="btnFormSign" type="submit">Envoyer</button>
          </form>
        </div>
    )
  }
}

export default Login;

Login.js with bind 带绑定的Login.js

postLogin = this.postLogin.bind(this);

async postLogin(e){

  await this.setState({
    [e.target.name]: e.target.value
  })

    e.persist(e);
    const login = {
        email:this.state.email,
        password:this.state.password
    };

  await axios.post("http://localhost:7777/api/users/login", login)
        .then((response) => {
            const {token} = response.data;
            localStorage.setItem("token", token)
            this.props.history.push("/articles")
        })

        .catch((error) => {
            console.log(error);
        })
}


  render(){

    return(
        <div className="containerSign">
          <h1>Connecte toi</h1>
          <form onSubmit={this.postLogin}>
            <input className="inputFormSign" type="email" placeholder="Email"/>
            <input className="inputFormSign" type="password" placeholder="Mot de passe"/>
            <button className="btnFormSign" type="submit">Envoyer</button>
          </form>
        </div>
    )
  }
}

export default Login;

You should use autobind decorator package to eradicate this issue! 您应该使用autobind装饰器包来消除此问题!

Installation: 安装:

npm install --save autobind-decorator

Examples: 例子:

import autobind from 'autobind-decorator';

@autobind
class Component { }

// or

@autobind
method() {
  return this.value
}

note: 注意:

To use ES7 decorators you must install babel-plugin-transform-decorators 要使用ES7 decorators您必须安装babel-plugin-transform-decorators

You must bind the method , for example using class property : 您必须绑定方法 ,例如使用类属性

async postLogin = (e) => {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM