2016-09-16 7 views
1

モデルのremoteMethod内でトークンのユーザーIDを回復しようとしています。ループバックでヘッダーにトークンを使用する

User.beforeRemote('**', function(ctx, user, next) { 
    //...some custom token decoding 
    ctx.req.body.user_id = token.user_id; 
    console.log('token : ', token); 
    next(); 
}); 

、その後、私のモデルの内部で私が使用:

User.check = function (user_id, cb) { 
    // Do stuff.. 
}; 


User.remoteMethod(
    'check', 
    { 
     http: {path: '/check', verb: 'post'}, 
     returns: {arg: 'rate', type: 'object'}, 
     accepts: [ 
     { 
      arg: 'user_id', 
      type: 'number', 
      required: true, 
      description: "id of the user to check", 
      http: function getUserid(ctx) { 
      console.log('User.check http body : ', ctx.req.body); 
      return ctx.req.body.user_id; 
      } 
     } 
     ], 
     description: '' 

    } 
); 

問題は私の引数の関数「getUseridは」「User.beforeRemote」呼び出し前にトリガーされることをである私は、次のコードを使用していました。

これはバグですか?どのように私はこの作品を作ることができるか考えていますか? 私は私が唯一のリモートメソッドでUSER_IDの引数を持つようにしたいから、と私は約20〜30の既存の方法で

ありがとうこれをしなければならないので、

arg : {http : {source : 'body'}}, 

を使用したくありません!私はミドルウェアで私のトークンデコードを追加しましたし、それは古典的な引数の型数で、うまく動作しますが、HTTP機能のない :

答えて

0

私は最終的にそれを行うための簡単な方法を発見した

//middleware dir : 
module.exports = function (app) { 
    "use strict"; 

    return function (req, res, next) { 
    //Custom token decoding code .... 
    //used req.body to show example with post requests : 
    req.body.user_id = parseInt(token.user_id); 

    console.log('token : ', token); 
    next(); 
    }; 
}; 

//remote Method declaration, without need to add a beforeRemote method : 
User.remoteMethod(
    'check', 
    { 
     http: {path: '/check', verb: 'post'}, 
     returns: {arg: 'rate', type: 'object'}, 
     accepts: [ 
     { 
      arg: 'user_id', 
      type: 'number', 
      required: true, 
      description: "id of the user to check" 
     } 
     ], 
     description: '' 
    } 
); 
関連する問題