简体   繁体   English

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

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

I'm trying to get a quick and dirty foundation for the authentication incorporated into an app using EAK. 我正在尝试为使用EAK集成到应用程序中的身份验证建立快速而肮脏的基础。 The authenticate function works as expected: it looks for a matching email and resolves the promise if it finds one. 身份验证功能按预期方式工作:它将查找匹配的电子邮件,并在找到该电子邮件时解决该诺言。

For some reason, the restore method is never being called on page reload...or at all. 由于某种原因,根本不会在页面重新加载时调用restore方法。 I don't know if this is an issue with EAK, ESA, or something else I'm doing wrong. 我不知道这是否与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);
    }
};

Any insights would be appreciated! 任何见解将不胜感激!

Edit: 编辑:

Here is the contents of local storage after initial authentication: 这是初始身份验证后本地存储的内容:

在此处输入图片说明

Edit: Shot of local scope after breakpoint 编辑:断点后本地范围的镜头

在此处输入图片说明

Edit: Added some debug lines to the restore method 编辑:添加一些调试行到还原方法

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();
      }
    });
  },

The 'Log 3' line is never being reached. 永远不会到达“ Log 3”行。 I also tried manually doing the _this.container.lookup('authenticator:custom') which seemed to cause any lines beyond it not to be reached. 我还尝试手动执行_this.container.lookup('authenticator:custom') ,这似乎导致无法到达该行之外的任何行。 So it seems there is a problem with the lookup. 因此,查找似乎有问题。

Edit: When the line container.injection('authenticator:custom', 'store', 'store:main') is removed from the initializer, the restore method gets called. 编辑:当从初始值设定项删除container.injection('authenticator:custom', 'store', 'store:main')行时,将调用restore方法。 Obviously, without the store, the authenticator is not very useful, so a different method of handling that may be needed. 显然,没有商店,身份验证器不是很有用,因此可能需要不同的处理方法。

And more: It seems any injection to the authenticator is causing this issue, not just the injection of the store. 还有更多:似乎对身份验证器的任何注入都会导致此问题,而不仅仅是商店的注入。

The problem was that I was attempting to inject a dependency before I called the Ember.SimpleAuth.setup(container, application) . 问题是我在调用Ember.SimpleAuth.setup(container, application)之前试图注入依赖项。 Here is the functioning initializer code: 这是起作用的初始化程序代码:

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');
    }
};

All you need to do is register your custom authenticator before simple-auth. 您需要做的就是在进行简单身份验证之前注册自定义身份验证器。

Basically, in your exported object add before: 'simple-auth' 基本上,在导出的对象中添加以下内容before: 'simple-auth'

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

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