2016-09-16 3 views
1

'remoteMethods 모델 내에서 토큰 사용자 아이디를 복구하는 데 문제가 있습니다.헤더의 루프백 토큰을 사용합니다.

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'호출하기 전에 트리거됩니다 있다는 것입니다 나는 다음과 같은 코드를 사용했다.

버그입니까? 내가 어떻게이 일을 할 수 있었는지 알았어? 내가 사용하지 않으려

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

난 단지 원격 메소드의 USER_ID의 인수를 갖고 싶어하기 때문에, 나는 약 20 ~ 30 기존의 방법

고맙습니다에서이 작업을 수행해야부터!

답변

0

내가 마침내 그 일의 쉬운 방법을 발견 : 내가 미들웨어 내 토큰 디코딩을 추가했고 그것이 HTTP 기능이없는, 고전 인수 유형 번호, 잘 작동 :

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