如何将用户存储在会话中

时间:2014-07-23 07:46:37

标签: ember.js django-rest-framework ember-simple-auth

我正在尝试使用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对象,所以如何解决它?

2 个答案:

答案 0 :(得分:14)

要标记@ marcoow的回复,以下是如何在Ember CLI中实现它:

的index.html:

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

初​​始化/定制-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);
  }
};

答案 1 :(得分: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',
}