如何使用Ember Simple Auth获取自定义会话的属性

时间:2014-12-31 16:08:42

标签: session ember.js ember-simple-auth authenticator

问题:我不知道如何在控制器中获取当前会话。

我有一个自定义身份验证器,自定义会话和初始化程序,如下所示:

来自../ app / authenticators / custom.js中的CUSTOM AUTHENTICATOR

var CustomAuthenticator = Base.extend({
  authenticate: function(credentials) {
    return new Ember.RSVP.Promise(function (resolve, reject){
      var loginPromise = Ember.$.post('/api/login', {'email':credentials.identification, 'password':credentials.password} );
      loginPromise.then(function (data){
        resolve({
            token: data.user.api_key,
            userData: data.user
        });
      }, function(error){
        reject(error);
      });
    });
  }
});

在... / app / sessions / custom.js中的CUSTOM SESSION

import Ember from 'ember';
import Session from 'simple-auth/session';

var CustomSession = Session.extend({
  after:'simple-auth',
  currentUser: function(){
    return this.container.lookup('ember_simple_auth:session');
  }.property('currentUser')
});

export default CustomSession;

INITIALIZER in ../ app / initializers / authentication.js

import CustomAuthenticator from '../authenticators/custom';
import CustomSession from '../sessions/custom';

export default {
  name:       'authentication',
  before:     'simple-auth',
  initialize: function(container) {
    container.register('authenticator:custom', CustomAuthenticator);
    container.register('session:custom', CustomSession);
  }
};

我尝试使用token在我的某个控制器中获取userDatathis.get('session'),但它给了我以下内容:

Class {store: Class, __ember1420041799205: "ember297", __nextSuper: undefined, __ember_meta__: Object, constructor: function…}

我在本地浏览器存储ember_simple_auth:session

中看到{"authenticator":"authenticator:custom","token":"123456789","userData":{"id":"1","email":"something@email.com","api_key":"123456789","expiry_time":"2014-12-31 14:02:56"}}键和值

我基本上需要了解本地存储中的内容。我该怎么做?

1 个答案:

答案 0 :(得分:0)

啊,我弄明白了这个问题。第一次进行身份验证时,会话变量就在那里,但刷新页面会删除会话的内容,因为我的身份验证器中没有恢复功能。

  restore: function(data) {
    return new Ember.RSVP.Promise(function (resolve, reject){
      console.log('RESTORE');
      if(!Ember.isEmpty(data.token)) {
        console.log('Found token: ' + data.token);
        resolve(data);
      } else {
        console.log('Token Not Found!');
        reject();
      }
    });
 }