简体   繁体   English

koa路由器和通行证链接结果进入回调

[英]koa-router and passport chaining results into callback

Alright , so I recently migrated from express to koa and my main question here is regarding koa-router and passport authentication. 好了,所以我最近从快递迁移到了koa,这里的主要问题是有关koa路由器和护照认证的。

Back in express i was able to do something like 回到快递中,我能够做类似的事情

router.route('/someroute')
      .post(passport.authenticate('local' , {session:false}) , 
                                  function(req,res,next){req.authresult});

what basically happened is once authenticated with the passport strategy , whatever object was returned in the strategy would be funneled into the REQ object where i could access it in the main controller function . 基本发生的事一旦通过护照策略进行了身份验证,在策略中返回的任何对象都将被集中到REQ对象中,我可以在主控制器函数中访问它。 Convenient right ? 方便吧?

Alright this is how koa does it , well afaik : 好吧,这就是koa的做法,afaik:

router.post('/someroute' , function(ctx,next){
return passport.authenticate('jwt' ,(err,user,info,status) =>{ 
           ....
 })(ctx);

Now that this situation is explained , my questions are : 现在已经说明了这种情况,我的问题是:

1. Is it possible to do the routing like express , something like 1.是否可以像express那样进行路由

router.post('/someroute',passport.authenticate(..),function(ctx){ ctx.user 
});

2. Why do i get a not found result if i don't pass the ctx object like 2.如果我不传递ctx对象,为什么会得到未找到的结果

passport.authenticate(...)(ctx);

What is this kind of implementation called , putting an object into a defined method .. where do i learn about it ? 将对象放入已定义的方法中,这种实现称为什么。我在哪里可以学到呢?

As usual, i have done some research and am ready to answer the question myself. 和往常一样,我已经做了一些研究,并准备自己回答这个问题。

1.Yes , it is possible to do the routing like express without declaring the custom callback for passport authenticate. 1.是的,可以进行诸如express之类的路由而无需声明护照身份验证的自定义回调。

router.post('../someroute',passport.authenticate('strategy',options),function(ctx){
//access passport return object through ctx.state.user
}

however the drawbacks of this are , if the strategy returns an error like done(err) , the error handling will not be done by your route callback rather will automatically be responded to with an internal server error by koa. 但是,这样做的缺点是,如果该策略返回一个诸如done(err)之类的错误,则错误处理将不会由您的路由回调完成,而是会由koa自动响应内部服务器错误。 If you wish to return an auth related error , it has to be done through the same ctx.state.user object with added properties . 如果要返回与auth相关的错误,则必须通过具有添加属性的同一个ctx.state.user对象来完成。

2.passport.authenticate() returns a function , that expects a req,res/ctx object therefore not providing that will result in an error (internal promise rejection,no ctx provided) . 2.passport.authenticate()返回一个函数,该函数期望一个req,res / ctx对象,因此不提供将导致错误(内部诺言拒绝,未提供ctx)。 In the above declaration , router.post handles this automatically.However , declaring it inside another function will require you to pass ctx. 在上面的声明中,router.post自动处理此问题,但是,在另一个函数中声明它需要您传递ctx。

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

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