Search code examples
sessionember.jsember-simple-auth

Ember.js - How can I save data to session from authenticator?


I have tried doing this.get('session') but it gives me nothing.

I want to save data to my session

I only seem to get the information I need from the authenticator but can't seem to be able to pass it around. (Tried a couple of methods suggested on SO but none seem to be able to work from the autheticator)

import Ember from 'ember';
  import Torii from 'ember-simple-auth/authenticators/torii';

  const { service } = Ember.inject;

  export default Torii.extend({
    torii: service('torii'),
    authenticate(options) {
      return this._super(options).then(function (data) {
        console.log(data);
      });
    }
  });

Caller of the autheticator (Is the info I need accessible from here already?)

import Ember from 'ember';

export default Ember.Controller.extend({
   session: Ember.inject.service('session'),
    actions: {
      authenticateSession() {
        this.get('session').authenticate('authenticator:torii', 'google-token');
      },
      invalidateSession() {
        this.get('session').invalidate();
      }
    }
});

Solution

  • Your authenticator's authenticate method does not resolve with anything. Change it to

    import Ember from 'ember';
    import Torii from 'ember-simple-auth/authenticators/torii';
    
    const { service } = Ember.inject;
    
    export default Torii.extend({
      torii: service('torii'),
      authenticate(options) {
        return this._super(options).then(function (data) {
          console.log(data);
          return data;
        });
      }
    });
    

    to have all attributes in data available via the session's data.authenticated property, e.g. this.get('session.data.authenticated.token').

    Of course in this case you can just remove the overridden authenticate method completely if you don't need the logging.