繁体   English   中英

EAK和ember-simple-auth自定义身份验证器

[英]EAK and ember-simple-auth custom authenticator

我正在尝试为使用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();
      }
    });
  },

永远不会到达“ Log 3”行。 我还尝试手动执行_this.container.lookup('authenticator:custom') ,这似乎导致无法到达该行之外的任何行。 因此,查找似乎有问题。

编辑:当从初始值设定项删除container.injection('authenticator:custom', 'store', 'store:main')行时,将调用restore方法。 显然,没有商店,身份验证器不是很有用,因此可能需要不同的处理方法。

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

问题是我在调用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');
    }
};

您需要做的就是在进行简单身份验证之前注册自定义身份验证器。

基本上,在导出的对象中添加以下内容before: 'simple-auth'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM