2016-08-08 47 views
0

我使用ember.js,簡單的身份驗證,並牌坊與Spotify的的OAuth。我目前可以登錄並註銷,只需查看資源,我就可以看到我正在從Spotify獲取訪問令牌。ember.js - 獲得訪問令牌,使用簡單的身份驗證/牌坊

當我登錄session.data作爲簡單的認證文檔說,我得到一個對象與我的所有數據,包括我的訪問令牌。但是,我似乎無法取出該訪問令牌。我試過session.data.authenticated只是訪問對象的下一級,但返回一個空對象。如果我嘗試去access_token,我得到undefinedcalling session.data.access_token

app/controllers/application.js

import Ember from 'ember' 

    export default Ember.Controller.extend({ 
     session: Ember.inject.service('session'), 

     actions: { 

     login() { 
      this.get('session').authenticate('authenticator:torii', 'spotify-oauth2-bearer') 
      console.log(this.get('session.data')) 
     }, 

     logout() { this.get('session').invalidate() } 
     } 

    }) 

app/authenticators/torii.js

import Ember from 'ember' 
    import ToriiAuthenticator from 'ember-simple-auth/authenticators/torii' 

    export default ToriiAuthenticator.extend({ torii: Ember.inject.service() }) 

我怎樣才能讓我的訪問令牌?

回答

1

session.data.authenticated在你登錄到控制檯的時間是不確定的。

this.get('session').authenticate(...)是異步,並返回一個承諾。通過在調用之後立即登錄到控制檯,認證可能尚未完成。

嘗試做:

this.get('session').authenticate(...).then(() => { 
    console.log(this.get('session.data.authenticated')); 
}); 
相關問題