Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在使用React重定向之前,请检查电子邮件/密码是否正确_Node.js_Reactjs_Api_Express - Fatal编程技术网

Node.js 在使用React重定向之前,请检查电子邮件/密码是否正确

Node.js 在使用React重定向之前,请检查电子邮件/密码是否正确,node.js,reactjs,api,express,Node.js,Reactjs,Api,Express,当用户使用电子邮件/密码成功验证时,我需要重定向到另一个页面(组件),否则,在div上显示一条警告消息,指出电子邮件或密码不正确 这是我的代码: constructor(props) { super(props); this.state = { // name: "", cnx_mail: '', cnx_pwd: '', joinByEmail: '', items: [], tok

当用户使用电子邮件/密码成功验证时,我需要重定向到另一个页面(组件),否则,在div上显示一条警告消息,指出电子邮件或密码不正确

这是我的代码:

constructor(props) {
    super(props);
    this.state = {
        // name: "",
        cnx_mail: '',
        cnx_pwd: '',
        joinByEmail: '',
        items: [],
        token: '',
        redirectToReferrer: false,
        errors: {},

        formErrors: {
            cnx_mail: '',
            cnx_pwd: ''
        },
        emailValid: false,
        passwordValid: false,
        formValid: false
    }
    this.handleUserInput = this.handleUserInput.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
}

handleSubmit = (event) => {
    event.preventDefault();
    fetch(`${API}/api/connexion`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                email: this.state.cnx_mail,
                password: this.state.cnx_pwd,
            })
        })
        .then(res => res.json())
        .then(json => {
            console.log(json);
            localStorage.setItem('toktok', json.result);
            //i have to verify email/password bedore redirection
            window.location.href = "/accounts";
        });
}
这是我在render()中的表单:


谢谢

到目前为止您尝试了什么?检查电子邮件和密码是否正确,角色==“admin”,然后重定向到/api/accounts,否则,如果电子邮件和密码正确,角色==“user”,则重定向到/api/account。谢谢
<form className="demoForm" onSubmit={this.handleSubmit} noValidate
    encType="application/x-www-form-urlencoded">
    <div className="alreadysubscribed-input">
        <div className={`alreadysubscribed-field group-input
            ${this.errorClass(this.state.formErrors.cnx_mail)}`}>
            <input type="email" required
                className="form-control fatb-input input-form" name="cnx_mail"
                value={this.state.cnx_mail} id="cnx_mail"
                onChange={this.handleUserInput} error={errors.cnx_mail} />
            <label className="fatb-label" htmlFor="cnx_mail">Email</label>
            <div className="fatb-bar"></div>
        </div>

        <div className={`alreadysubscribed-field group-input
            ${this.errorClass(this.state.formErrors.cnx_pwd)}`}>
            <input type="password" required
                className="form-control fatb-input input-form" name="cnx_pwd"
                value={this.state.cnx_pwd} id="cnx_pwd"
                onChange={this.handleUserInput} error={errors.cnx_pwd} />
            <label className="fatb-label" htmlFor="cnx_pwd">Mot de passe</label>
            <div className="fatb-bar"></div>
        </div>
    </div>
    <FormErrors formErrors={this.state.formErrors} />

    <div className="btn-cnx">
        {/* <span className="mas">Se connecter</span> */}
        <button className="fatb-btn bubbly-button btn-anim3 w100p" type="submit"
            name="cnx_btn" disabled={!this.state.formValid}>se
            connecter</button>
    </div>
</form>
{
    "success": true,
    "message": "SUCCESS",
    "result": [
        {
            "dateCreation": "2019-10-09T21:54:18.491Z",
            "resetpasswordtoken": null,
            "resetpasswordexpires": null,
            "role": "admin",
            "enable": true,
            "_id": "5d9e67772682971c1fc3711c",
            "email": "meachemlal@gmail.com",
            "password": "$2b$10$HqEMSkaWBZ..BFvSpxliUu/02vWodvxYoFZMaYUU5ZsBeqeZB4jLu",
            "tickets": [],
            "gains": [],
            "__v": 0,
            "nom": "ACHEMLAL",
            "prenom": "MERYEM",
            "tel": "+33619562721"
        },
        {
            "dateCreation": "2019-10-09T21:54:18.491Z",
            "resetpasswordtoken": null,
            "resetpasswordexpires": null,
            "role": "admin",
            "enable": true,
            "_id": "5d9e677e2682971c1fc3711d",
            "email": "mika.achemlal@gmail.com",
            "password": "$2b$10$Bg4nOrNMzc.b/JBNKNiRP.BiMd/.h4CHKiy0nvV/uim0d3VwHl9iq",
            "tickets": [],
            "gains": [],
            "__v": 0
        },
    ........