Search code examples
javascriptember.jsember-simple-authtorii

ember.js - getting access token using simple auth/torii


I am using ember.js, simple auth, and torii for oauth with Spotify. I can currently log in and log out just fine, and looking at resources I can see I am getting an access token from Spotify.

When I log session.data as the simple auth docs say, I am getting an Object with all my data in it, including my access token. However, I can't seem to pull out that access token. I have tried session.data.authenticated to just access the next level of the object, but that returns an empty object. If I try to get to the access_token, I get undefined calling 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() })

How can I get to my access token?


Solution

  • session.data.authenticated is undefined at the time you are logging to console.

    this.get('session').authenticate(...) is async and returns a promise. By logging to the console right after the call, the authentication might not have completed yet.

    Try doing:

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