问题
I am new to Hapi.js.I am using "hapi-auth-jwt2" module for authentication token and role verification. I set the scope and sent that scope from the callback of validateFunc . It will worked very well for checking te role based authentication. But i want the result i am returning from the validateFunc but don't know where i can get that.
validateFunc: function (token, request, callback) {
Async.auto({
session: function (done) {
Session.findByCredentials(token.sessionId, token.sessionKey, done);
},
user: ['session', function (results, done) {
if (!results.session) {
return done();
}
User.findById(results.session.user, done);
}],
}, (err, results) => {
if (err) {
return callback(err);
}
if (!results.session) {
return callback(null, false);
}
results.scope = token.scope;
callback(null, Boolean(results.user), results);
});
}
});
};
`
It verify the scope or Role in the domain i.e:-
routeOptions: {
scope:{
createScope:"admin"
},
create: {
pre : function(payload, Log){
console.log("preee runnnig........");
console.log(payload);
}
}
I am getting the payload Json what i am sending from the client side but i want the results i am sending from the callback of validateFunc, because i want to use that data here in pre prior to send the request.I am working on implicitly created API via Rest Hapi Module.
So how can i get that datain pre hooks from the validateFunc . Any help is much appreciated.
Thanks
回答1:
This is actually a feature that is being worked on and hopefully will be done soon.
For now, you can omit the generated create endpoint and replace it with your own in order to access the request object.
The resulting code would look something like this:
'use strict';
const RestHapi = require('rest-hapi');
module.exports = function (server, mongoose, logger) {
server.route({
method: 'POST',
path: '/pathName',
config: {
handler: function(request, reply) {
/* modify request.payload here and access auth info through request.auth.credentials */
const Model = mongoose.model('modelName');
return RestHapi.create(Model, request.payload, logger)
.then(function (result) {
return reply(result);
})
.catch(function (error) {
return reply(error);
});
},
tags: ['api'],
plugins: {
'hapi-swagger': {}
}
}
});
};
来源:https://stackoverflow.com/questions/44546596/how-to-get-result-of-validatefunc-in-pre-of-auto-created-api-rest-hapi