2016-06-03 3 views
0

인스턴스 초기화 프로그램, 서비스 및 ember-simple-auth 라이브러리를 사용하여 현재 사용자를 얻는 데 다음과 같은 문제점이 있습니다. 더 구체적으로 말하자면, 로그인 한 후 페이지를 새로 고칠 때만 내 서비스 "사용자"의 모든 속성을 가져올 수있었습니다. 로그인 후 즉시 내 서비스로 돌아올 수 있다는 약속을드립니다. 내 코드 조각을 사용할 다음과 같습니다 :/Ember 2.4 - 로그인 후 서비스 및 간단한 인증을 사용하여 현재 사용자 얻기

export function initialize(appInstance) { 

    appInstance.inject('route', 'account', 'service:account'); 
    appInstance.inject('controller', 'account', 'service:account'); 
    appInstance.inject('template', 'account', 'service:account'); 
} 

export default { 
    name: 'account', 
    initialize: initialize 
}; 

응용 프로그램/서비스

응용 프로그램/인스턴스 초기화/전류의 user.js 나는 그런

import Ember from 'ember'; 

export default Ember.Service.extend({ 

session: Ember.inject.service('session'), 
store: Ember.inject.service('store'), 

loadCurrentUser() { 

    const accountId = this.get('session.data.authenticated.auth_token'); 
    console.log("This is my account", accountId); 

    if (!Ember.isEmpty(accountId)) { 

     this.get('store').findRecord('profile', accountId).then((profile) => { 
      this.set('user', profile); 
      console.log("account_group",this.get('user.user_group')); 
      console.log("account_username",this.get('user.username')); 
     }); 
    } 
    return this.get('user')    
} 
}); 

을 account.js 내 응용 프로그램 경로에서 loadCurrentUser 함수를 호출하십시오.

import Ember from 'ember'; 
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin'; 

export default Ember.Route.extend(ApplicationRouteMixin,{ 

    beforeModel(){ 
    console.log("This is my currentUser", this.get("account").loadCurrentUser()); 
}, 

}); 
,

그리고 마지막으로 내 템플릿에이 같은 내 사용자의 사용자 이름에 액세스 할 수 있습니다

Logged in as: {{account.user.username}} 

하지만 난 내 페이지를 새로 고침 만.

내가 "loadCurrentUser()"함수를 잘못된 위치로 호출한다고 가정하지만 적절한 해결책을 찾을 수 없습니다. 미리 감사드립니다.

답변

1

Authenticator가 약속을 해결할 때 loadCurrentUser()으로 전화해야한다고 생각합니다. 다음과 같음 :

that.get('session').authenticate('authenticator:xxxx',{}).then(() => { 
    this.get("account").loadCurrentUser(); 
}); 
+0

답장을 보내 주셔서 감사합니다. 사용자의 로그인 후에도 잘 작동했지만 페이지를 새로 고칠 때 작동하려면 application.js 경로에 코드를 보관해야합니다. 나는 당신이 그것에 관한 힌트를 줄 수 있다면 감사하는 것 이상이다. – Jack

+0

첫 번째 생각으로, 나는 beforeModel() {this.get ("account")처럼 페이지 새로 고침을위한 애플리케이션 경로에 코드를 배치 할 것이다. loadCurrentUser(); }'서비스에'init' 메소드를 추가하십시오.'init() {this._super (... arguments); this.loadCurrentUser(); }'('_super() '에 대한 호출을 잊지 말것 – GUL

+0

피드백에 대해 많은 고마워, 귀하의 의견은 정말로 가치가있었습니다! – Jack