如何在会话中存储用户 [英] How to store the user in a session

查看:23
本文介绍了如何在会话中存储用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to set up ember-simple-auth with a django-rest-framework backend, but I'm running into some trouble saving the user to the session. I have to be able to do something like this in my templates:

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

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

So following several guides I found, I have got the authentication and authorization working so that I can get a valid token and use is in requests. To get the user on the session, I have modified App.CustomAuthenticator.authenticate so that when the token is returned, the username is also stored to the session:

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 属性:

I then modified Application.intializer to give session a user property:

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}} 被渲染时,它只是一个空字符串.我的问题是:

However, when {{session.user.username}} is rendered it is just an empty string. My questions are:

  1. 这真的是将用户分配到会话的最佳方式吗?这对我来说似乎很笨拙,但我看不到任何更好的东西.
  2. 我认为空字符串是因为返回的是 Promise 而不是 User 对象,那么我该如何解决?
  1. Is this really the best way to assigning a user to the session? It seems clumsy to me but I can't see anything better.
  2. I assume that the empty string is because a Promise is returned rather than a User object, so how to I resolve it?

推荐答案

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

With the 0.6.4 release you can now specify a custom session class without having to reopen, see release note here: https://github.com/simplabs/ember-simple-auth/releases/tag/0.6.4. This is how it works:

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',
}

这篇关于如何在会话中存储用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆