{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/loopback/issues/1495","repository_url":"https://api.github.com/repos/strongloop/loopback","labels_url":"https://api.github.com/repos/strongloop/loopback/issues/1495/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/loopback/issues/1495/comments","events_url":"https://api.github.com/repos/strongloop/loopback/issues/1495/events","html_url":"https://github.com/strongloop/loopback/issues/1495","id":92444733,"number":1495,"title":"Injecting Remote Context via Options","user":{"login":"ritch","id":462228,"avatar_url":"https://avatars.githubusercontent.com/u/462228?v=3","gravatar_id":"","url":"https://api.github.com/users/ritch","html_url":"https://github.com/ritch","followers_url":"https://api.github.com/users/ritch/followers","following_url":"https://api.github.com/users/ritch/following{/other_user}","gists_url":"https://api.github.com/users/ritch/gists{/gist_id}","starred_url":"https://api.github.com/users/ritch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ritch/subscriptions","organizations_url":"https://api.github.com/users/ritch/orgs","repos_url":"https://api.github.com/users/ritch/repos","events_url":"https://api.github.com/users/ritch/events{/privacy}","received_events_url":"https://api.github.com/users/ritch/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/strongloop/loopback/labels/%23tob","name":"#tob","color":"eeeeee"},{"url":"https://api.github.com/repos/strongloop/loopback/labels/feature","name":"feature","color":"bfe5bf"},{"url":"https://api.github.com/repos/strongloop/loopback/labels/major","name":"major","color":"eb6420"}],"state":"open","locked":false,"assignee":{"login":"ritch","id":462228,"avatar_url":"https://avatars.githubusercontent.com/u/462228?v=3","gravatar_id":"","url":"https://api.github.com/users/ritch","html_url":"https://github.com/ritch","followers_url":"https://api.github.com/users/ritch/followers","following_url":"https://api.github.com/users/ritch/following{/other_user}","gists_url":"https://api.github.com/users/ritch/gists{/gist_id}","starred_url":"https://api.github.com/users/ritch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ritch/subscriptions","organizations_url":"https://api.github.com/users/ritch/orgs","repos_url":"https://api.github.com/users/ritch/repos","events_url":"https://api.github.com/users/ritch/events{/privacy}","received_events_url":"https://api.github.com/users/ritch/received_events","type":"User","site_admin":false},"assignees":[{"login":"ritch","id":462228,"avatar_url":"https://avatars.githubusercontent.com/u/462228?v=3","gravatar_id":"","url":"https://api.github.com/users/ritch","html_url":"https://github.com/ritch","followers_url":"https://api.github.com/users/ritch/followers","following_url":"https://api.github.com/users/ritch/following{/other_user}","gists_url":"https://api.github.com/users/ritch/gists{/gist_id}","starred_url":"https://api.github.com/users/ritch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ritch/subscriptions","organizations_url":"https://api.github.com/users/ritch/orgs","repos_url":"https://api.github.com/users/ritch/repos","events_url":"https://api.github.com/users/ritch/events{/privacy}","received_events_url":"https://api.github.com/users/ritch/received_events","type":"User","site_admin":false}],"milestone":null,"comments":59,"created_at":"2015-07-01T17:47:02Z","updated_at":"2016-08-03T06:43:52Z","closed_at":null,"body":"The example below allows you to inject the remote context object (from strong remoting) into all methods that accept an `options` argument.\r\n\r\n```js\r\n\r\nfunction inject(ctx, next) {\r\n  var options = hasOptions(ctx.method.accepts) && (ctx.args.options || {});\r\n  if(options) {\r\n    options.remoteCtx = ctx;\r\n    ctx.args.options = options;\r\n  }\r\n  next();\r\n}\r\n\r\napp.remotes().before('*.*', inject);\r\n\r\napp.remotes().before('*.prototype.*', function(ctx, instance, next) {\r\n  inject(ctx, next);\r\n});\r\n\r\n// unfortunately this requires us to add the options object\r\n// to the remote method definition\r\napp.remotes().methods().forEach(function(method) {\r\n  if(!hasOptions(method.accepts)) {\r\n    method.accepts.push({\r\n      arg: 'options',\r\n      type: 'object',\r\n      injectCtx: true\r\n    });\r\n  }\r\n});\r\n\r\nfunction hasOptions(accepts) {\r\n  for (var i = 0; i < accepts.length; i++) {\r\n    var argDesc = accepts[i];\r\n    if (argDesc.arg === 'options' && argDesc.injectCtx) {\r\n      return true;\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nWhy? So you can use the remote context in remote methods, operation hooks, connector implementation, etc.\r\n\r\n```js\r\nMyModel.observe('before save', function(ctx, next) {\r\n  console.log(ctx.options.remoteCtx.accessToken.userId); // current user\r\n});\r\n```\r\n\r\nThis approach is specifically designed to allow you to do what is possible with `loopback.getCurrentContext()` but without the dependency on `cls`. The injection approach is much simpler and should be quite a bit faster since `cls` adds some significant overhead.\r\n\r\n@bajtos @fabien @raymondfeng "},"comment":{"url":"https://api.github.com/repos/strongloop/loopback/issues/comments/237155922","html_url":"https://github.com/strongloop/loopback/issues/1495#issuecomment-237155922","issue_url":"https://api.github.com/repos/strongloop/loopback/issues/1495","id":237155922,"user":{"login":"dongmai","id":3855966,"avatar_url":"https://avatars.githubusercontent.com/u/3855966?v=3","gravatar_id":"","url":"https://api.github.com/users/dongmai","html_url":"https://github.com/dongmai","followers_url":"https://api.github.com/users/dongmai/followers","following_url":"https://api.github.com/users/dongmai/following{/other_user}","gists_url":"https://api.github.com/users/dongmai/gists{/gist_id}","starred_url":"https://api.github.com/users/dongmai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dongmai/subscriptions","organizations_url":"https://api.github.com/users/dongmai/orgs","repos_url":"https://api.github.com/users/dongmai/repos","events_url":"https://api.github.com/users/dongmai/events{/privacy}","received_events_url":"https://api.github.com/users/dongmai/received_events","type":"User","site_admin":false},"created_at":"2016-08-03T06:43:52Z","updated_at":"2016-08-03T06:43:52Z","body":"How can I access the remote context in a  middle ware, it just have `(req, res, next)` ?\r\n\r\n\r\n"}}
{"actor":{"display_login":"dongmai"}}