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

余生长醉 提交于 2020-01-16 22:00:23

问题


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();
      }
    }
});

回答1:


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.



来源:https://stackoverflow.com/questions/35262515/ember-js-how-can-i-save-data-to-session-from-authenticator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!