gpt4 book ai didi

node.js - hapi-auth-cookie 未设置 cookie

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:39 25 4
gpt4 key购买 nike

对于我的 Node 应用程序,我使用 bell 和 hapi-auth-cookie 插件来使用 Yahoo api。使用当前代码,我能够通过雅虎进行身份验证,然后重定向到主页。然而,一旦我进入主页,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();
});

最佳答案

阐述并扩展 Eran 的答案:

如果您想访问不需要身份验证即可查看的路由(例如主页)的身份验证/ session 数据,这是可能的,但在我看来不是很直观。您必须在路由上设置身份验证方案,然后将模式更改为“尝试”,并设置特定于路由的 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 进行身份验证,则可以使用通过 hapi-auth-cookie 设置的 cookie 数据。

关于node.js - hapi-auth-cookie 未设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25355508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com