如何在注册和登录表单中添加适当的验证?

时间:2020-01-21 15:45:05

标签: node.js mongodb express

    registerUser: (req, res, next) => {
        const { username, email, password } = req.body
        User.create(req.body, (err, createdUser) => {
            if (err) {
                return next(err)
            } else if (!username || !email || !password) {
                return res.status(400).json({ message: "Username, email and password are must" })
            } else if (!validator.isEmail(email)) {
                return res.status(400).json({ message: "Invaid email" })
            } else if (password.length < 6) {
                return res.status(400).json({ message: "Password should be of at least 6 characters" })
            }
            else {
                return res.status(200).json({ user: createdUser })
            }
        })
    },

    loginUser: (req, res, next) => {
        const { email, password } = req.body
        User.findOne({ email }, (err, user) => {
            if (err) {
                return next(err)
            } else if (!user || !password) {
                return res.status(400).json({ message: "Email and password are must" })
            } else if (!validator.isEmail(email)) {
                return res.status(400).json({ message: "Invalid email" })
            } else if (!user) {
                return res.status(402).json({ error: "User not found" })
            } else if (!user.confirmPassword(password)) {
                return res.status(402).json({ error: "Incorrect password" })
            }

            // generate token here
            const token = auth.signToken(email)
            res.status(200).json({ user, token })
        })
    },

我想检查用户是否已经在数据库中,那么该用户将可以登录。如果没有,则消息将为“您尚未注册”。使用不同于已注册电子邮件的电子邮件登录,这表示必须输入电子邮件和密码。

总而言之,我需要在注册和登录表单中添加适当的验证。

    handleSubmit = (event) => {
        event.preventDefault();
        const { email, password } = this.state;

        const loginData = {
            email: this.state.email,
            password: this.state.password
        }

        if (!email || !password) {
            return alert('Email and password are must.');
        }

        if (password.length < 6) {
            return alert('Password must contain 6 characters.');
        }

        if (!validator.isEmail(email)) {
            return alert('Invalid email.');
        }

        this.props.dispatch(loginUser(loginData))
        this.props.history.push("/")
    }

在前端,我将用户登录后重定向到主页。现在的问题是,无论凭证是什么,用户都将进行重定向。我是否也必须在前端在此处添加验证?

2 个答案:

答案 0 :(得分:0)

(!user || !password)的意思是“如果找不到用户或请求正文中没有密码”。如果要具体检查是否未找到用户,则应该使用if(!user)并对其执行操作...已经执行的操作-在else if语句中降低。但是第一个条件(if(!user || !password))是第二个条件(if(!user ))的超集,因此您永远也不会到达第二个条件。您应该将此条件移到序列中的第一个。

通常,我会重做整个if语句,以将某些条件嵌套在其他条件中,例如

// user found:
if(user) {
    if(!user.confirmPassword(password)) {
        // user found but password is wrong
    }
} else {
    // user not found
}

答案 1 :(得分:0)

因此,您的问题是,在loginUser中,条件达到!user之前,它将首先进入!user || !password。这就是为什么它总是说“必须输入电子邮件和密码”的原因。

您的逻辑将是这样的:

  • 如果用户未提供任何电子邮件或密码,则应立即返回“必须输入电子邮件和密码”,您不再希望检入数据库了,因为他们没有提供数据库搜索的任何意义。
  • 如果用户确实提供了电子邮件和密码,那么您现在可以进行数据库检查,如果在搜索数据库后与用户提供的内容不匹配,则只需返回“找不到用户”或按照“您尚未注册”进行操作即可

代码:

loginUser: (req, res, next) => {
    const { email, password } = req.body
    if (!email || !password) {
        // user does not provide email or password, return immediately without db search
        return res.status(400).json({ message: "Email and password are must" })
    }
    User.findOne({ email }, (err, user) => {
        if (err) {
            return next(err)
        } else if (!validator.isEmail(email)) {
            return res.status(400).json({ message: "Invalid email" })
        } else if (!user) {
            // user provide email and password however email deos not match any in db
            // this is equals to "You are not registered"
            return res.status(402).json({ error: "User not found" })
        } else if (!user.confirmPassword(password)) {
            return res.status(402).json({ error: "Incorrect password" })
        }

        // generate token here
        const token = auth.signToken(email)
        res.status(200).json({ user, token })
    })
},

希望这就是您想要的。

相关问题