快速路由:如何将用户带到可选的验证流程并返回?

Express routing: How to take user to an optional validation flow and back?

我有一个 Node Express 网络应用程序,我需要在其上实现以下流程:

  1. /product/:productId/buy: User submits a form with Name, Address and phone number that I submit to my API layer

  2. API responds with 201 for All good, and 202 for Order submitted, but we need to verify your mobile number first

  3. /product/:productId/confirm: If 201, show user the confirmation screen

  4. /verify/phone: If 202, take user to a phone verification screen that says: Verify your phone number: <insert phone number entered at step#1> with the OTP sent to your phone

  5. /product/:productId/confirm: User enters OTP. Hit API, verify and take them to the confirmation screen. Else reload /verify/phone

我设置了以下路由:

    {
        method: 'GET',
        path: '/product/: productId/buy',
        action: ['productController', 'getBuyForm']
    },
    {
        method: 'POST',
        path: '/product/: productId/buy',
        action: ['productController', 'postBuyForm']
    },
    {
        method: 'GET',
        path: '/verify/phone',
        action: ['verificationController', 'getVerificationForm']
    },
    {
        method: 'POST',
        path: '/verify/phone',
        action: ['verificationController', 'postVerificationForm']
    }

当我 POST postBuyForm 时,我在请求正文中提交 phoneaddressname

服务器以空主体和状态代码响应。

现在,如果状态代码是 202,我需要导航到 verify/phone 页面,但我还需要以某种方式继承我提交的 phonepostBuyForm 因为我需要在页面上显示它。

执行此操作的一个选项是使用:

res.redirect('verify/phone/'+phone)

但我的公司不希望 phone 数字成为查询字符串的一部分,因为这会导致滥用。

另一种选择是使用@circusbred 在下面提到的会话:

app.post('/product/: productId/buy', (req, res) => {
  req.session.phone = phone;
});

app.get('/verify/phone', (req, res) => {
  console.log(req.session.phone);
});

但我们尽量不使用会话,因为我们的设计只对经过身份验证的用户使用会话,并且此功能也必须适用于未经过身份验证的用户。

我的问题是:

有没有一种方法可以导航或重定向到验证页面,同时传递 phone 值而不必将其包含在查询字符串中?

您唯一可行的选择是利用某种 sessions

app.post('/product/: productId/buy', (req, res) => {
  req.session.phone = phone;
});

app.get('/verify/phone', (req, res) => {
  console.log(req.session.phone);
});