如何使用Django-rest和React js验证用户

时间:2019-03-27 07:48:56

标签: reactjs django-rest-framework django-rest-auth

我正在尝试使用django rest和不带有redux的简单react js来对用户进行身份验证

用户已在django rest端点(电子邮件,密码)成功通过身份验证,但是当我尝试从react进行身份验证时,它没有显示任何结果,我无法找到它。从react端,我通过表单获取用户输入并保存进入状态并使用POST方法将状态发送到终结点。但是在Django结束时什么都没发生。以下是我的逻辑:

class login extends React.Component{
  constructor(props){
    super(props);
    this.state={
        email:"",
        password:"",
    }
}

submitForm=(evt)=>{
    evt.preventDefault();
    const form= {
        email: this.state.email,
        password: this.state.password
    };

    this.loginUser(form)
};

loginUser=(payload)=>{
    console.log("In fetch request");
    console.log(payload);
    fetch(`http://127.0.0.1:8000/chauffeur/login_user/`,
        {
            method:'POST',
            body: JSON.stringify(payload),
            headers:{
                'Content-Type':'application/json'
            }
        }).then(response =>{
            if (response.status===200){
                return response.json().then(data=>{
                    return {status:"ok",data}
                })
            }
            else
            {
                console.log("Unable to access")
            }
    }).catch(error => console.error('Error:', error))

    setInputValue = (e, inputName) => {
    this.setState({[inputName]: e.target.value})
};

render(){
    return(
        <div>
            <form onSubmit={this.submitForm}>
                <input type='email' placeholder="email" value={this.state.email} onChange={(e) => this.setInputValue(e, 'email')}/>
                <input type='password' placeholder="password" value={this.state.password} onChange={(e) => this.setInputValue(e, 'password')}/>
                <button type="submit">SignIn</button>
            </form>
        </div>
    )
}

我尝试在请求后看到json响应,但控制台上什么都没有。请给出一些解决方法。

0 个答案:

没有答案