向Everyauth添加另一个步骤的最佳方法是什么?

时间:2011-11-08 21:24:52

标签: javascript node.js everyauth

我想在everyauth openid module中定义的OpenId身份验证序列中添加另一个步骤。

我不确定Everyauth是否是为此而设计的。作者mentions它是可自定义的,但没有示例,我仍然是一个javascript newb。

例如,everyauth中的OAuth模块定义了其身份验证回调步骤,如下所示:

.get('callbackPath',                                                                                                                                               
     'the callback path that the 3rd party OAuth provider redirects to after an OAuth authorization result - e.g., "/auth/facebook/callback"')                     
  .step('getCode')
    .description('retrieves a verifier code from the url query')                                                                                                   
    .accepts('req res')  
    .promises('code')                                                       
    .canBreakTo('authCallbackErrorSteps')
  .step('getAccessToken')
    .accepts('code')
    .promises('accessToken extra')                                                                                                                                 
  .step('fetchOAuthUser')
    .accepts('accessToken')                                                                                                                                        
    .promises('oauthUser')
  .step('getSession')
    .accepts('req')
    .promises('session')
  .step('findOrCreateUser')                                                                                                                                        
    .accepts('session accessToken extra oauthUser')                                                                                                                
    .promises('user')
  .step('compile')
    .accepts('accessToken extra oauthUser user')
    .promises('auth')
  .step('addToSession')
    .accepts('session auth')                                                                                                                                       
    .promises(null)
  .step('sendResponse')
    .accepts('res')
    .promises(null)

如果我需要额外的自定义步骤,我该怎么办?我宁愿不改变everyauth模块的源代码。

2 个答案:

答案 0 :(得分:4)

很长一段时间,这个问题被提出但没有答案。我刚刚遇到了同样的挑战并记录了我的答案,以获得像我这样的其他节点新手的好处:)

我使用Facebook身份验证,并希望在成功进行身份验证后将响应发送到客户端之前执行一些自定义处理。我没有添加另一个步骤,而是覆盖了步骤" sendResponse',添加了我的自定义逻辑,然后只委托给了超级发送响应'实现。现在我的代码看起来像 -

everyauth
    .facebook
    .sendResponse(function(res) {
        console.log('##### sendResponse custom step called #####');
        // --> Your custom logic here
        this._super();
    });

如上所示,您可以为特定模块(如Facebook)覆盖它,也可以在“每个模块”中完成。水平。

希望这有帮助。

答案 1 :(得分:0)

另一个选项是addToSession步骤。它可以在facebook,linkedin和密码模块中使用。我发现sendResponse不能用于密码模块。如果您的自定义逻辑依赖于会话变量,这将简化事情。