How to get result of validateFunc in pre of auto created API Rest Hapi

依然范特西╮ 提交于 2019-12-12 23:41:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!