hapi-auth-cookie不设置cookie

对于我的节点应用即时通讯使用铃和hapi-auth-cookie插件来使用雅虎api。 使用当前的代码,我可以用雅虎进行身份validation,然后redirect到主页。 但是,一旦我到达主页,request.auth似乎是空的。 从我所知道的情况来看,我正在做所有的事情,但是一旦我回到主页,我就没有authentication了。 任何帮助表示赞赏! 这是我得到的:

var Path = require('path'); var Hapi = require('hapi'); var cookieSession = require('cookie-session'); var serverOptions = { views: { engines: { html: require('handlebars') }, path: Path.join(__dirname, './app/www/public/pages'), layoutPath: Path.join(__dirname, './app/www/public/pages') } }; var server = new Hapi.Server(8003, serverOptions); server.pack.register([ require('bell'), require('hapi-auth-cookie') ], function(err) { if (err) { throw err; } server.auth.strategy('yahoo', 'bell', { provider: 'yahoo', password: 'cookie_encryption_password', clientId:'2kj3kj2', clientSecret: '3kj2k3jl', isSecure: false // Terrible idea but required if not using HTTPS }); server.auth.strategy('session', 'cookie', { password: 'secret', cookie: 'sid-example', redirectTo: '/login', isSecure: false }); server.route({ method: ['GET', 'POST'], // Must handle both GET and POST path: '/login', // The callback endpoint registered with the provider config: { auth: 'yahoo', handler: function (request, reply) { var creds = request.auth.credentials; request.auth.session.clear(); request.auth.session.set(creds); return reply.redirect('/'); } } }); server.route({ method: 'GET', path: '/', handler: function (request, reply) { reply.view('index', { title: 'hello world' }); } }); server.start(); }); 

为了阐述和扩展伊兰的答案:

如果你想访问authentication/会话数据的路由不需要身份validation来查看(如主页),这是可能的,但不是很直观,在我看来。 您必须在路由上设置authscheme,但是将模式更改为“try”,并设置路由特定的hapi-auth-cookie参数,以防止未经身份validation的用户被redirect到login页面,如下所示:

 server.route({ method: 'GET', path: '/', config: { handler: homepage, auth: { mode: 'try', strategy: 'session' }, plugins: { 'hapi-auth-cookie': { redirectTo: false } } } }); 

mode: 'try'将允许用户进入路由path,即使未经过身份validation, redirectTo: false将停止未经身份validation的路由请求redirect到login页面。 通过这种方式,用户可以在不进行身份validation的情况下访问此路由(通常用于主页),但一旦通过hapi-auth-cookievalidation了cookie数据集即可使用。

您的主页缺less身份validation。 您需要configuration“/”才能使用您的Cookieauthenticationscheme。

即使正确设置了一些东西,我最近在Facebook和Twitter上看到了一些问题(所以我可以看到雅虎也一样),具体取决于使用的是哪个版本的Bell(4.0确实与Facebook有问题),以及调用是否来自node_modules或不。 听起来很疯狂,这些问题可以在Clapper的最新版本中看到,其中hapi-bell-auth-cookie-plugin使用完全相同的方法(但不是作为node_module)正常工作。