Store session in operation hook - Loopback

╄→尐↘猪︶ㄣ 提交于 2019-11-29 17:23:10
imran ali

try this now :

you can set ctx value :

var LoopBackContext = require('loopback-context');

    MyModel.myMethod = function(cb) {
      var ctx = LoopBackContext.getCurrentContext();
      // Get the current access token
      var accessToken = ctx && ctx.get('accessToken');
      ctx.set('xx', { x: 'xxxx' } );

    }

it's get ctx value :

 module.exports = function(MyModel) {
      MyModel.observe('access', function(ctx, next) {
        const token = ctx.options && ctx.options.accessToken;
        const userId = token && token.userId;
        const modelName = ctx.Model.modelName;
        const scope = ctx.where ? JSON.stringify(ctx.where) : '<all records>';
        console.log('%s: %s accessed %s:%s', new Date(), user, modelName, scope);
        next();
      });
    };

loopback context store userId and accesTokan. in whole web you can access using ctx it's work like session in loopback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!