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

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

问题描述

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

 < h2>欢迎回来,{{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'
})然后(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.intializer 给出会话 a 用户属性:

  Ember.Application.initializer({
name:'authentication',
before:'simple-auth',
initialize:function(container,应用程序){
//注册自定义验证器和授权器,因此Ember Simple Auth可以找到
container.register('authentator:custom',App.CustomAuthenticator);
container.register('授权者: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. 我假设空字符串是因为承诺答复而不是用户对象,那么如何解决呢?


解决方案

您现在可以指定自定义会话类而无需重新打开,请参阅发行说明: 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',
}
/ pre>

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>

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

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

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

  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?

解决方案

With the latest 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天全站免登陆