我将 Hapi 框架 (nodejs) 与Bell模块一起使用,并与 Twitter 提供商合作。
使用 github 页面中给出的示例获取工作代码非常简单。我访问/login路由并被重定向到 Twitter,我在其中授权应用程序,然后我被重定向回/login?oauth_token=xxxxxxx&oauth_verifier=xxxxxxx在那里我可以访问 request.auth.credentials 中的用户配置文件.
当我试图拒绝该应用程序时,问题就来了。我没有单击 Twitter 上的“登录”按钮,而是单击了“取消”按钮,然后单击了“返回站点名称”按钮。最后一个按钮将我重定向到/login?denied=xxxxxx,然后我(再次)被重定向到 Twitter 以批准该应用程序。
我尝试使用同一页面https://github.com/hapijs/bell#handling-errors中的另一个示例来处理这种情况,但无法使其正常工作。
server.route({
method: ['GET', 'POST'],
path: '/login',
config: {
auth: {
strategy: 'twitter',
mode: 'try'
},
handler: function (request, reply) {
if (!request.auth.isAuthenticated) {
return reply('Authentication failed due to: ' + request.auth.error.message);
}
return reply.redirect('/home');
}
}
});
似乎在检查 request.auth 之前,它会解释 /login 路由并重定向到 Twitter。我仍然不太了解 Bell 模块,但可能是 Twitter 策略期望 request.params 中的oauth_token和oauth_verifier,但是策略没有解释被拒绝的参数,这就是重定向发生的原因。
有人设法处理这种情况吗?