Store session in operation hook - Loopback

前端 未结 1 1306
-上瘾入骨i
-上瘾入骨i 2020-12-22 08:46

I want to store some data other than userId or accessToken to store in a session, in after save or before save operation

相关标签:
1条回答
  • 2020-12-22 09:02

    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.

    0 讨论(0)
提交回复
热议问题