2014-07-23 1 views
5

django-rest-framework 백엔드로 ember-simple-auth를 설정하려고하는데 세션에 사용자를 저장하는 데 문제가 있습니다. 내 템플릿에서 같은 것을 할 수 있어야 : 내가 찾은사용자를 세션에 저장하는 방법

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

그래서 다음과 같은 몇 가지 가이드, 내가 유효한 토큰을 얻을 수 있습니다 및 사용 요청에 있도록 작업 인증 및 권한 부여를 가지고있다.

하십시오 user 속성을 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.intializer 수정 : 토큰이 반환 될 때, 사용자 이름도 세션에 저장되도록 세션에서 사용자를 얻으려면, 내가 App.CustomAuthenticator.authenticate을 수정 한

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. 빈 문자열은 User 개체 대신 Promise가 반환되기 때문에 가정합니다. 어떻게 해결할 수 있습니까?
+0

지금은 실제로 그렇게하는 방법입니다. Ember Simple Auth의 다음 릴리스에서는 자신의 세션 클래스를 지정할 수 있고 더 이상 기존 세션을 다시 열지 않아도되므로 쉽게 얻을 수 있습니다. – marcoow

+0

자, 그렇다면 실제 'User'를 어떻게 얻을 수 있습니까? – aquavitae

+0

checkout 정확하게 원하는 Repo의 예 : https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html – marcoow

답변

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

는 @의 marcoow의 응답 오프에 태그를 여기, 엠버 CLI에서이를 구현하는 방법은 다음과 같습니다 : 이것은 어떻게 작동

index.html을 :

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

초기화/사용자 세션을 .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); 
    } 
}; 
+0

최종 구현을 제공해 주셔서 감사합니다. 나는 똑같은 상황이었다. 고마워. –

+0

오늘 도움이 될 수 있습니다. http://stackoverflow.com/questions/30872684/ember-simple-auth-injecting-current-user-into-every-route – Toni