Javascript 快速用户认证中间件,它应该做多少?

Javascript 快速用户认证中间件,它应该做多少?,javascript,express,middleware,Javascript,Express,Middleware,我正在尝试学习Express会话和身份验证处理 例如: app.post('/login', authCredentials, function(req, res) { console.log("second") }); function authCredentials(req, res, next) { //this happens first console.log(req.body) // => { username: etc, password: etc } n

我正在尝试学习Express会话和身份验证处理

例如:

app.post('/login', authCredentials, function(req, res) {
   console.log("second")
});

function authCredentials(req, res, next) {
  //this happens first
  console.log(req.body) // => { username: etc, password: etc }
  next(); 
}
我的问题是我的
authCredentials
函数应该做多少? 例如,如果凭据正确,我可以执行以下操作
res.redirect('/index')
。但是,一旦我这样做了,第二个函数有什么用途

其他问题:

  • 如何处理无效凭据
  • 如果我创建了
    authcidentifications
    只需根据凭据返回
    true
    false
    ,这是否会中断中间件流,因为它永远不会调用
    next()
  • 在它之后,是否可以访问匿名回调中的
    authCredentials
    中的任何内容?基本上在
    函数(req,res){}

  • 您希望将您的
    authCredentials
    中间件添加到每个需要身份验证的端点
    app.post(“/login”)
    通常不需要任何密码,因为您首先要访问此端点才能实际获取凭据

    当凭证正确/有效时,只需调用
    next()
    ,工作流将跳转到下一个中间件或实际端点。如果出现错误,请使用错误对象调用
    next()
    ,例如
    next(新错误('not authenticate');
    。将错误路由添加到常规路由中,错误将在那里处理:

    app.use(function(err, req, res, next) {
        res.render('error', err);
    });
    
  • 现在应该可以回答了
  • 中间件不返回值。它要么调用
    next()
    ,要么通过调用
    res.send()
    以不同方式结束进程
  • 将变量从一个中间件传递到另一个中间件有不同的方法。最常见的方法可能是将所需的值附加到
    req
    参数
  • authenticate
    在以下示例中是一个非同步函数:

    function authCredentials(req, res, next) {
        authenticate(req.body, function(err, user) {
            if (err) {
                return next(err);
            }
            req.user = user;
            next();
        });
    }
    

    答案取决于您的身份验证策略,即您是否使用会话标识符、访问令牌等

    在这两种情况下,我建议您从身份验证中断凭证交换(aka login)

    function usernamePasswordExchange(req,res,next){
      var username = req.body.username;
      var password = req.body.password;
    
      callToAuthService(username,password,function(err,user){
        if(err){
          next(err); // bad password, user doesn’t exist, etc
        }else{
          /*
            this part depends on your application.  do you use
            sessions or access tokens?  you need to send the user
            something that they can use for authentication on
            subsequent requests
          */
          res.end(/* send something */);
        }
      });
    }
    
    function authenticate(req,res,next){
      /*
        read the cookie, access token, etc.
        verify that it is legit and then find
        the user that it’s associated with
      */
      validateRequestAndGetUser(req,function(err,user){
        if(err){
          next(err); // session expired, tampered, revoked
        }else{
          req.user = user;
          next();
        }
      });
    }
    
    app.post('/login',usernamePasswordExchange);
    
    app.get('/protected-resource',authenticate,function(req,res,next){
      /*
        If we are here we know the user is authenticated and we
        can know who the user is by referencing req.user
      */
    });
    
    免责声明:我在工作,我们花了很多时间写作 验证码:)我刚写了我们最新的库,
    它具体实现了我的建议

    为了澄清,你的第一个例子是
    app.use(函数(err,req,res,next){res.render('error',err);})
    只是演示一种处理错误参数的方法,对吗?在第二个示例中,
    returnnext(err),返回下一个中间件函数?错误处理中间件需要4个参数,而不是通常的3个参数。如果使用参数调用
    next
    ,则会自动调用错误中间件。如果您有不同的错误处理程序,则调用它们的顺序与使用
    app.use定义的顺序相同。有关更多信息,请点击以下链接:好的,调用
    next(err)
    会自动在中间件中查找错误。但是为什么我们要返回next(err)
    ?我猜
    authenticate
    函数接收
    req.body
    ,它具有凭证,检查凭证,然后调用回调,但这就是我对
    返回next(err)会发生什么感到困惑的地方
    return
    只是一个小技巧,可以阻止整个函数在那里执行。当出现错误时,我们不希望继续使用
    req.user=user等等。当然,您可以将该部分包装在
    else
    语句中,并省略
    return
    。这只是为了更好的可读性。哦,还有,我忘了我也很困惑为什么我不应该在
    app.post('/login')
    上进行身份验证。我的逻辑是,那个特定的路由只有在到达那个端点的帖子上才会被激活。除此之外,
    req.body
    何时将用户凭据发送到端点?