简体   繁体   English

使用 express.js 向同一个 url 发送多个发布请求

[英]Sending multiple post requests to same url using express.js

on my website I have a login form where the user inputs their information and when they submit a post request is made where their info is checked, and if it is valid I redirect them back to the login form where they then enter the code that was sent to their email. My question is when the post request is made the second time how would I use the same url but not have to go through the validating again.在我的网站上,我有一个登录表单,用户可以在其中输入他们的信息,当他们提交发布请求时,会检查他们的信息,如果有效,我会将他们重定向回登录表单,然后他们在其中输入代码发送到他们的 email。我的问题是当第二次发出发布请求时,我将如何使用相同的 url 而不必再次通过验证 go。

Auth.js授权.js

//Login Route
router.post('/login', async(req, res) => {
    //Validate Data
    const { error } = loginValidation(req.body);

    if (error) {
        let msg = error.message;
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;

    }
    //Check if user exists
    const user = await User.findOne({ email: req.body.email });
    if (!user) {
        let msg = 'Email or password is invalid!'
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
    }


    //Check if password is correct
    const validPass = await bcrypt.compare(req.body.password, user.password);
    if (!validPass) {
        let msg = 'Email or password is invalid!'
        return res.cookie('loginError', msg, [{ httpOnly: true }]).redirect('/login');;
    }

    const verificationCode = Math.floor(100000 + Math.random() * 900000);
    email.loginCode(req.body.email, verificationCode);
    return res.cookie('formComplete', 'true', [{ httpOnly: true }]).redirect('/login');
    //Create and assign a jwt
    const token = jwt.sign({ _id: user._id }, process.env.TOKEN_SECRET);
    res.header('auth-token', token).redirect('/dashboard');
});

Sorry if this is a dumb question, i'm new to express.js, if you need any more info let me know.抱歉,如果这是一个愚蠢的问题,我是 express.js 的新手,如果您需要更多信息,请告诉我。

Actually, it's not really related to express.实际上,它与 express 并没有真正的关系。
You are asking about "state" in the server.您在询问服务器中的“状态”。

If you was to "refactor" your question, it could become "I have this user that's signed in, how to verify him before he visit any routes"如果你要“重构”你的问题,它可能会变成“我有这个登录的用户,如何在他访问任何路线之前验证他”

The solution can be several解决方案可以是几个

  1. Add a global state. Let say isVerified = false, after verification for the first time, you set it to true添加一个全局的state。假设isVerified = false,第一次验证后,你设置为true
  2. As you see in my "refactor" question above, session is something you use to check user logged in or shopping cart...正如你在我上面的“重构”问题中看到的, session是你用来检查用户登录或购物车的东西......

I suggest you dig into both solutions above, for example for the first one, where to declare that global state?我建议你深入研究上面的两个解决方案,例如第一个,在哪里声明全局 state? What if we need to check multiple users?如果我们需要检查多个用户怎么办?

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

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