0
votes

How is it possible to access the loopback context (or simple Express req object) from within the model's logic?

It is critical to be able to know more about the request itself (current user identity more than anything else) inside the model's logic. When I override a built-in method (via custom script or from the model.js file) or develop a custom remote method, I would like to access the Express req object.

As loopback.getCurrentContext() is declared to be buggy, I cannot use it.

Ideas?

PS: I find this page confusing: http://loopback.io/doc/en/lb2/Using-current-context.html

First it's said (and marked in red as important!) it is not recommended to use LoopBackContext.getCurrentContext() and then it's used it in each example!?

What's the point to give examples that do not work? Should we simply ignore the complete page? If so, what about the context? :)

Any clarification on this topic is much appreciated.

2

2 Answers

0
votes

You can get access to express req object by using remote hooks

var loopback = require('loopback');
module.exports = function (MyModel) {

 MyModel.beforeRemote('findOne', function (ctx, model, next) {
   //access to ctx.req
   console.log(ctx.req.headers) 
   next()         
 })

 MyModel.beforeRemote('my-custom-remote-method', function (ctx, model, next) {
   console.log(ctx.req.headers)
   next()
 }) 

}
0
votes

Sure, you can use a beforeRemote hook to modify the ctx.args property. This property is the input of the remote method (that is, custom or built-in). This way, you can copy a part of the request inside this property, and it will be passed to the build-in method

Example 1 with the built-in method findOne.

MyModel.beforeRemote('findOne', function (ctx, model, next) {
   ctx.args.filter.extrafield = ctx.req.headers['some-header'];
   next();         
 });

Then override the findOne method since it's what you want to do

MyModel.on('dataSourceAttached', function(obj){
    var findOne = MyModel.findOne;
    MyModel.findOne = function(filter, cb) {
      console.log(filter.extrafield); // Print what was in the header
      return findOne.apply(this, arguments);
    };
  });

And finally call the method with curl

curl -H "some-header: 'hello, world!'" localhost:3000/api/MyModel/findOne

Example 2 with a custom remote printToken, to help you understand further

MyModel.beforeRemote('printToken', function (ctx, model, next) {
  ctx.args.token = ctx.req.headers['some-header'];
  next();         
});

MyModel.printToken = function(token, cb) {
  console.log(token);
  cb();
}

MyModel.remoteMethod(
  'printToken', 
  {
    accepts: {arg: 'token', type: 'string', optional: true}
  }
);

Then call the remote with curl, and pass the expected header

 curl -H "some-header: 'hello, world!'" localhost:3000/api/MyModel/printToken

EDIT: There is a simpler solution that only works for custom remote

When defining your remote method, it is possible to tell loopback to pass elements of the http request to your remote directly as an input argument

MyModel.remoteMethod(
  'printToken', 
  {
    accepts: [
     {arg: 'req', type: 'object', 'http': {source: 'req'}},
     {arg: 'res', type: 'object', 'http': {source: 'res'}}
    ]
  }
);

This way, your remote can access the req and res objects. This is documented here