5

我正在尝试使用 django-rest-framework 后端设置 ember-simple-auth,但是在将用户保存到会话时遇到了一些麻烦。我必须能够在我的模板中做这样的事情:

<h2>Welcome back, {{session.user}}</h2>

因此,按照我找到的几个指南,我已经完成了身份验证和授权工作,以便我可以获得有效的令牌并在请求中使用。为了让用户进入会话,我进行了修改App.CustomAuthenticator.authenticate,以便在返回令牌时,用户名也存储到会话中:

authenticate: function(credentials) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
        Ember.$.ajax({
            url: _this.tokenEndpoint,
            type: 'POST',
            data: JSON.stringify({username: credentials.identification, password: credentials.password }),
            contentType: 'application/json'
        }).then(function(response) {
            Ember.run(function() {
                resolve({
                    token: response.token,
                    username: credentials.identification
                });
            });
        }, function(xhr, status, error) {
            var response = JSON.parse(xhr.responseText);
            Ember.run(function() {
                reject(response.error);
            });
        });
    });
},

然后我修改Application.intializersession一个user属性:

Ember.Application.initializer({
    name: 'authentication',
    before: 'simple-auth',
    initialize: function(container, application) {
        // register the custom authenticator and authorizer so Ember Simple Auth can find them
        container.register('authenticator:custom', App.CustomAuthenticator);
        container.register('authorizer:custom', App.CustomAuthorizer);
        SimpleAuth.Session.reopen({
            user: function() {
              var username = this.get('username');
              if (!Ember.isEmpty(username)) {
                return container.lookup('store:main').find('user', {username: username});
              }
            }.property('username')
        });
    }
});

但是,在{{session.user.username}}渲染时它只是一个空字符串。我的问题是:

  1. 这真的是将用户分配到会话的最佳方式吗?这对我来说似乎很笨拙,但我看不到更好的东西。
  2. 我假设空字符串是因为返回了 Promise 而不是User对象,那么我该如何解决呢?
4

2 回答 2

14

要标记 @marcoow 的响应,以下是在 Ember CLI 中实现它的方法:

索引.html:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:withCurrentUser'
};

初始化程序/customize-session.js:

import Session from 'simple-auth/session';

var SessionWithCurrentUser = Session.extend({
  currentUser: function() {
    var userId = this.get('user_id');
    if (!Ember.isEmpty(userId)) {
      return this.container.lookup('store:main').find('user', userId);
    }
  }.property('user_id')
});

export default {
  name: 'customize-session',
  initialize: function(container) {
    container.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};
于 2014-08-14T22:41:29.907 回答
4

使用 0.6.4 版本,您现在可以指定自定义会话类而无需重新打开,请参阅此处的发布说明:https ://github.com/simplabs/ember-simple-auth/releases/tag/0.6.4 。这是它的工作原理:

App.CustomSession = SimpleAuth.Session.extend({
  account: function() {
    var accountId = this.get('account_id');
    if (!Ember.isEmpty(accountId)) {
      return this.container.lookup('store:main').find('account', accountId);
    }
  }.property('account_id')
});
…
container.register('session:custom', App.CustomSession);
…
window.ENV['simple-auth'] = {
  session: 'session:custom',
}
于 2014-07-26T08:29:33.940 回答