如何在自动创建的API Rest Hapi之前获取validateFunc的结果 [英] How to get result of validateFunc in pre of auto created API Rest Hapi

查看:68
本文介绍了如何在自动创建的API Rest Hapi之前获取validateFunc的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hapi.js的新手.我正在使用"hapi-auth-jwt2" 模块进行身份验证令牌和角色验证.我设置了作用域,并从validateFunc的回调中发送了该作用域.它对于检查基于角色的身份验证非常有效.但是我想要我从 validateFunc 返回的结果,但不知道我在哪里可以得到.

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

我从客户端发送的是我收到的有效负载Json,但我希望从validateFunc的回调中发送结果,因为我想在发送请求之前在此使用该数据.我正在通过Rest Hapi模块处理隐式创建的API.

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.

那么我该如何从validateFunc的预钩中获取该数据.非常感谢您的帮助.

So how can i get that datain pre hooks from the validateFunc . Any help is much appreciated.

谢谢

推荐答案

这实际上是一项正在开发的功能,希望很快会完成.

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.

结果代码如下:

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

这篇关于如何在自动创建的API Rest Hapi之前获取validateFunc的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆