1
我在Hapi.js中实施身份验证方案。Hapi身份验证方案:设置自定义标头
在我的authenticate
函数中,我验证了该请求并想设置自定义标题。但是因为我必须使用reply.continue()
完成身份验证功能,所以我无法将任何标头传递给响应。
如何将自定义标题传递给客户端?
最小代码:
var Boom = require('boom'),
Hoek = require('hoek'),
request = require('request');
exports.register = function(plugin, config, next) {
plugin.auth.scheme('myScheme', function(server, options) {
Hoek.assert(options, 'Missing auth strategy options');
return {
authenticate: function(req, reply) {
request(
'http://localhost/somewhere',
function(error, response, body) {
if (error) {
return reply(null, null, Boom.unauthorized(null, 'myScheme'));
}
options.validateFunc(
body,
function(validateError, isValid, credentials) {
if (validateError || !isValid) {
return reply(
Boom.unauthorized('Invalid cookie'),
null,
{credentials: credentials}
);
}
// I want to add a custom header here
//.header('my-header', 'my-header-content')
return reply
.continue({
credentials: credentials || body
}));
}
);
}
);
}
};
});
next();
};
exports.register.attributes = {
pkg: require('../package.json')
};