【问题标题】:EAK and ember-simple-auth custom authenticatorEAK 和 ember-simple-auth 自定义身份验证器
【发布时间】:2014-05-28 19:46:52
【问题描述】:

我正在尝试为使用 EAK 将身份验证合并到应用程序中建立一个快速而肮脏的基础。验证功能按预期工作:它查找匹配的电子邮件并在找到时解析承诺。

由于某种原因,restore 方法永远不会在页面重新加载时被调用......或者根本不会调用。我不知道这是 EAK、ESA 的问题还是我做错的其他事情。

var customAuthenticator =  Ember.SimpleAuth.Authenticators.Base.extend({
    resourceName: 'user',
    identifierField: 'email',
    restore: function (data) {
        console.log('Attempt to restore -> ', data);
        return new Ember.RSVP.Promise(function (resolve, reject) {
            resolve(data);
        });
    },
    authenticate: function (credentials) {
        var self = this;
        return new Ember.RSVP.Promise(function (resolve, reject) {
            var idObject = {};
            idObject[self.identifierField] = credentials.identification;

            self.get('store').find(self.resourceName, idObject).then(function (user) {
                var dataId;
                if(user.get('content').length) {
                    dataId = user.get('content').objectAt(0).get('data').id;
                    resolve({userId: dataId});
                } else {
                    reject();
                }
            });
        });
    },
    invalidate: function () {
        console.log('Attempt to invalidate');
        return new Ember.RSVP.Promise(function (resolve, reject) {
            resolve();
        });
    }
});

export default {
    name: 'authentication',
    initialize: function (container, application) {
        Ember.SimpleAuth.Session.reopen({
            user: function() {
                if (!Ember.isEmpty(this.get('userId'))) {
                    return container.lookup('store:main').find('user', +this.get('userId'));
                }
            }.property('userId')
        });
        container.register('authenticator:custom', customAuthenticator);
        container.injection('authenticator:custom', 'store', 'store:main');
        Ember.SimpleAuth.setup(container, application);
    }
};

任何见解都将不胜感激!

编辑:

这里是初始认证后本地存储的内容:

编辑:断点后局部范围的镜头

编辑:在恢复方法中添加了一些调试行

restore: function() {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      var restoredContent      = _this.store.restore();
      var authenticatorFactory = restoredContent.authenticatorFactory;
      if (!!authenticatorFactory) {
        console.log('Log 1');
        delete restoredContent.authenticatorFactory;
        console.log('Log 2');
        _this.container.lookup(authenticatorFactory).restore(restoredContent).then(function(content) {
          console.log('Log 3');
          _this.setup(authenticatorFactory, content);
          resolve();
        }, function() {
          _this.store.clear();
          reject();
        });
      } else {
        _this.store.clear();
        reject();
      }
    });
  },

永远不会到达“日志 3”行。我还尝试手动执行_this.container.lookup('authenticator:custom'),这似乎导致无法到达超出它的任何行。所以看起来查找有问题。

编辑:container.injection('authenticator:custom', 'store', 'store:main') 行从初始化程序中删除时,会调用restore 方法。显然,没有 store,authenticator 就不是很有用,因此可能需要一种不同的处理方法。

还有更多:似乎对身份验证器的任何注入都会导致此问题,而不仅仅是商店的注入。

【问题讨论】:

  • 会话验证后能否查看存储(本地存储)中的内容?
  • @marcoow 我已将商店的照片添加到主帖
  • 你能在github.com/simplabs/ember-simple-auth/blob/master/packages/…这里设置一个断点并检查发生了什么吗?
  • @marcoow 看来商店在该断点(或断点后的行)处仍然具有相同的 KV 对,并且在 restoreContent 对象中存在authenticatorFactory 和userId。为了安全起见,我将添加一个屏幕截图。
  • 那么它会调用验证者的restore 方法吗? ID authenticatorFactory 存在,它实际上应该存在。

标签: javascript ember.js ember-app-kit ember-simple-auth


【解决方案1】:

问题是我在调用Ember.SimpleAuth.setup(container, application) 之前尝试注入依赖项。下面是功能初始化代码:

export default {
name: 'authentication',
initialize: function (container, application) {
    Ember.SimpleAuth.Session.reopen({
        user: function() {
            if (!Ember.isEmpty(this.get('userId'))) {
                return container.lookup('store:main').find('user', +this.get('userId'));
            }
        }.property('userId')
    });
    container.register('authenticator:custom', customAuthenticator);
    Ember.SimpleAuth.setup(container, application);
    container.injection('authenticator:custom', 'store', 'store:main');
    }
};

【讨论】:

    【解决方案2】:

    您需要做的就是在 simple-auth 之前注册您的自定义身份验证器。

    基本上,在您导出的对象中添加before: 'simple-auth'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多