request.auth.session.set(user_info)不起作用HapiJS [英] request.auth.session.set(user_info) not working HapiJS
问题描述
这是我的策略,它在server.register()上定义.我的工作基于一个教程,它实际上是从教程中复制出来的,但是不起作用.
This is my strategy, which is defined on a server.register(). I'm basing my work off a tutorial and it is literally copied from it but it doesn't work.
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']
});
这是我的登录路线,发生错误:
This is my login route where the error occurs:
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());
// });
}
}
});
我已经尝试了很多事情,但这部分对于这项工作至关重要,我找不到任何遇到相同问题的人.请帮忙!
I've tried so many things but this part is crucial for this work and I can't find anyone with the same problem. Help please!
推荐答案
hapi-auth-cookie更改了设置和清除cookie的方式.从5.0.0版开始,请使用request.cookieAuth.set()和request.cookieAuth.clear().您可能使用的插件版本比本教程的package.json中使用的插件版本新.
hapi-auth-cookie has changed the way cookies are set and cleared. As of version 5.0.0, use request.cookieAuth.set() and request.cookieAuth.clear(). You are probably using a more recent version of the plugin than is used in the package.json of the tutorial.
来源: https://github.com/hapijs/hapi-auth-cookie/commit/d233b2a3e4d0f0362453b91b
这篇关于request.auth.session.set(user_info)不起作用HapiJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!