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')
};