Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the validateFunc returned data in pre hook of internal API made by Rest-Hapi. #19

Closed
techloverparveen opened this issue Jun 15, 2017 · 4 comments

Comments

@techloverparveen
Copy link

techloverparveen commented Jun 15, 2017

Hi, I am just wondering to know that,

How can we add some extra info in PAYLOAD object in auth (validateFunc) function so that we can use that in pre (create) hook in internal API (generated by Rest-Hapi). Because as i want some user info for some validation in my pre hook, like:-

Schema.statics = {
		collectionName: modelName,
		routeOptions: {
			scope:{
				createScope:"Occupant"
			},
create: {
        pre : function(payload, Log){
        	console.log(payload);	// here i get only payload sent from client side but i want some other info that returned from the validateFunc of "hapi-auth-jwt2"
        }
}
}

but as i only able to get the payload there so how can i get the additional info as well.
You can get the detail question i posted over stackoverflow:-
https://stackoverflow.com/questions/44546596/how-to-get-result-of-validatefunc-in-pre-of-auto-created-api-rest-hapi
Please help me regarding this ASAP. Thanks

@JKHeadley
Copy link
Owner

Hi @parveen-oodles, 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': {}
        }
      }
    });
};

@techloverparveen
Copy link
Author

Thanks JKHeadley for your quick response. I really appreciate that.
Thanks again.:)

@JKHeadley
Copy link
Owner

JKHeadley commented Jun 20, 2017 via email

@techloverparveen
Copy link
Author

techloverparveen commented Jun 21, 2017

Thanks JKHeadley, yes i have resolved my issue, that was due to the standalone endpoint wasn't in the separate file. I have another query regarding Rest-Hapi would appreciate if you please tell me that via association there are some more API created for that particular model, but as Rest-Hapi create API for them is that also expose some internal methods to do the same work via server side i mean like there are method like restHapi.create(modelName, data, log), that will do the same work that the create API endpoint do. So like this is rest-hapi also expose some methods at server side for association API made internally by that.

for example :- /building/{ownerId}/occupant.

(This API is generated by rest Hapi Association), so how can i do the same in server side is there any method expose at server side like this :-

restHapi.create(building,ownerId,data,log)

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants