request.auth.session.set(user_info)不工作HapiJS

这是我的策略,它是在server.register()上定义的。 我把我的工作从一个教程,这是从字面上复制,但它不起作用。

server.auth.strategy('standard', 'cookie', { password: 'somecrazycookiesecretthatcantbeguesseswouldgohere', // cookie secret cookie: 'app-cookie', // Cookie name isSecure: false, // required for non-https applications redirectTo: '/login', ttl: 24 * 60 * 60 * 1000 // Set session to 1 day }); server.auth.default({ strategy: 'standard', mode: 'required', scope: ['admin'] }); 

这是我的login路线发生错误:

 server.route({ method: 'POST', path: '/login', config: { auth: false, validate: { payload: { email: Joi.string().email().required(), password: Joi.string().min(2).max(200).required() } }, handler: function (request, reply) { getValidatedUser(request.payload.email, request.payload.password) .then(function (user) { if (user) { //ERROR OCCURS HERE: IT SAYS SESSION IS UNDEFINED request.auth.session.set(user); return reply('Login Successful!'); } else { return reply(Boom.unauthorized('Bad email or password')); } }); // .catch(function (err) { // return reply(Boom.badImplementation()); // }); } } }); 

我已经尝试了很多东西,但这部分对于这项工作至关重要,我找不到任何人有同样的问题。 请帮助!

hapi-auth-cookie改变了cookies的设置和清除方式。 从版本5.0.0开始,使用request.cookieAuth.set()和request.cookieAuth.clear()。 您可能使用的是本教程的package.json中使用的更新版本的插件。

来源: https : //github.com/hapijs/hapi-auth-cookie/commit/d233b2a3e4d0f03ef53b91b7929b8dbadbff624c