【问题标题】:hapi-auth-cookie not setting cookiehapi-auth-cookie 未设置 cookie
【发布时间】:2014-10-10 21:44:59
【问题描述】:

对于我的节点应用程序,我使用 bell 和 hapi-auth-cookie 插件来使用 Yahoo api。使用当前代码,我可以通过 Yahoo 进行身份验证,然后被重定向到主页。但是,一旦我进入主页, request.auth 似乎是空的。据我所知,我所做的一切都与示例完全相同,但是一旦我回到主页,我就没有身份验证。任何帮助表示赞赏!这是我得到的:

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();
});

【问题讨论】:

    标签: node.js authentication cookies


    【解决方案1】:

    阐述和扩展 Eran 的答案:

    如果您想访问不需要身份验证即可查看的路由(例如主页)的身份验证/会话数据,这是可能的,但在我看来不是很直观。您必须在路由上设置身份验证方案,然后将模式更改为“尝试”,并设置特定于路由的 hapi-auth-cookie 参数,以防止未经身份验证的用户被重定向到登录页面:

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

    mode: 'try' 将允许用户在未经身份验证的情况下继续访问路由路径,redirectTo: false 将停止对重定向到登录页面的路由的未经身份验证的请求。这样,用户无需身份验证即可到达此路由(通常用于主页),但一旦通过 hapi-auth-cookie 进行身份验证,cookie 数据集就可以使用了。

    【讨论】:

    • 您的回答让我走上了正确的道路。为了扩展它,现在可以使用 redirectOnTry 选项(hapi 版本 6.2.0+)为 configured globally 提供此行为。
    【解决方案2】:

    您的主页缺少身份验证。您需要配置 '/' 以使用您的 cookie 身份验证方案。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 2016-02-24
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多