hapi及其auth-cookie插件的例子并不多,但这是我迄今为止尝试确保路由的例子.请注意,我见过的大部分示例都使用的是较旧版本的hapi,这似乎并不适用于这种情况,我希望我只是遗漏了一些简单的东西:
var Hapi = require('hapi');
var Mongoose = require('mongoose');
Mongoose.connect('mongodb://localhost/rfmproducetogo');
var server = new Hapi.Server(8080, "localhost");
server.pack.register([{
plugin: require("lout")
}, {
plugin: require('hapi-auth-cookie')
}, {
plugin: require("./plugins/togo")
}, {
plugin: require("./plugins/auth")
}], function(err) {
if (err) throw err;
server.auth.strategy('session', 'cookie', {
password: 'shhasecret',
cookie: 'wtfisthisfor',
isSecure: false,
redirectTo: false
});
server.start(function() {
console.log("hapi server started @ " + server.info.uri);
});
});
Run Code Online (Sandbox Code Playgroud)
在我的togo插件中,我有这个路由设置来使用会话
exports.create = function(plugin) {
plugin.route({
method: 'POST',
path: '/togo/add',
handler: function(request, reply) {
produce = new Produce();
produce.label = request.payload.label;
produce.price = request.payload.price;
produce.uom = request.payload.uom;
produce.category = request.payload.category;
produce.save(function(err) {
if (!err) {
reply(produce).created('/togo/' + produce._id);
} else {
reply(err);
}
});
},
config: {
auth: 'session'
}
});
};
Run Code Online (Sandbox Code Playgroud)
我看到的错误是这样的:
/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421
throw new Error(msgs.join(' ') || 'Unknown error');
^
Error: Unknown authentication strategy: session in path: /togo/add
at Object.exports.assert (/home/adam/Projects/bushhog/node_modules/hapi/node_modules/hoek/lib/index.js:421:11)
at /home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:123:14
at Array.forEach (native)
at internals.Auth._setupRoute (/home/adam/Projects/bushhog/node_modules/hapi/lib/auth.js:121:24)
at new module.exports.internals.Route (/home/adam/Projects/bushhog/node_modules/hapi/lib/route.js:118:43)
at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:110:25
at Array.forEach (native)
at /home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:107:17
at Array.forEach (native)
at internals.Router.add (/home/adam/Projects/bushhog/node_modules/hapi/lib/router.js:104:13)
Run Code Online (Sandbox Code Playgroud)
运行节点0.10.28,hapijs 6.x,hapi-auth-cookie 1.02
当您尝试在身份验证策略实际可用之前使用该策略时,就会出现此问题。
\n\n您已经通过将功能拆分为具有给定范围的单独的小插件来遵循良好的应用程序设置。
\n\n更新:这里有一个针对该问题的专用教程,如何修复 \xe2\x80\x9eunknown 身份验证策略\xe2\x80\x9c
\n\n设置身份验证和依赖身份验证的插件的一个好方法是创建一个额外的“身份验证插件”,它添加您所需的策略并可以用作其他插件的依赖项。
\n\n\n\nexports.register = function (server, options, next) {\n\n // declare/register dependencies\n server.register(require(\'hapi-auth-cookie\'), err => {\n\n /**\n * Register authentication strategies to hapi server\n *\n * We\xe2\x80\x99re using hapi-auth-cookie plugin to store user information on\n * client side to remember user data on every website visit\n *\n * For sure, we could and will add more authentication strategies.\n * What\xe2\x80\x99s next: JWT (we highly welcome pull requests to add JWT functionality!)\n */\n server.auth.strategy(\'session\', \'cookie\', {\n password: \'ThisIsASecretPasswordThisIsASecretPassword\',\n cookie: \'hapi-rethink-dash\',\n redirectTo: \'/login\',\n isSecure: false\n });\n\n server.log(\'info\', \'Plugin registered: cookie authentication with strategy \xc2\xbbsession\xc2\xab\')\n\n next()\n\n })\n\n}\n\nexports.register.attributes = {\n name: \'authentication\',\n version: \'1.0.0\'\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在你的/plugins/togo设置中,你将authentication plugin设为依赖项(使用server.dependency([array-of-deps])),这意味着 hapi 首先注册 auth 插件,然后注册依赖项。
您可以像这样注册您的插件:
\n\nserver.register([{\n plugin: require(\'./plugins/authentication\')\n}, {\n plugin: require("./plugins/togo")\n}], function(err) { \n // handle callback\n})\nRun Code Online (Sandbox Code Playgroud)\n\n检查hapi-rethinkdb-dash以获取详细示例。
\n\n希望有帮助!
\n| 归档时间: |
|
| 查看次数: |
1311 次 |
| 最近记录: |