jeudi 7 juillet 2016

Ember-simple-auth: Use Custom cookie session to store additional token

Using: ember-cli v2.5, ember-simple-auth v1.1.0

I got trouble understanding if I can properly store additional token inside a custom ember-simple-auth's cookie based session-store.

I'm trying to store a shopping-cart token to be sure it survives browser refresh.

I started to create a shopping-cart service to handle init, add, remove etc ... regarding if the session has a token.

Here is my app/session-stores/application.js

// app/session-stores/application.js
import Cookie from 'ember-simple-auth/session-stores/cookie';

export default Cookie.extend({
    orderToken: null
});

Doesn't seems to be used. The session service still use the adaptive store.

Here is my shopping-cart service

// app/services/shopping-cart.js
import Ember from 'ember';

export default Ember.Service.extend({
  store: Ember.inject.service('store'),
  session: Ember.inject.service('session'),
  basket: null,

  [...]

  init() {
    this._super(...arguments);
    let orderToken = this.get('session.orderToken'); // First try
    // let orderToken = this.get('session.store.orderToken'); // Second Try
    if (orderToken) {
      this.get('store').findRecord('order', orderToken).then((order) => {
        this.set('basket', order);
      })
    }
  },

  [...]

  _createBasket() {
    return this.get('store').createRecord('order', {}).save().then((order) => {
      this.set('basket', order);
      this.set('session.orderToken', order.token); // First try
      // this.set('session.store.orderToken', order.token); // Second Try
    });
  }
})

Then the idea will be to inject the service wherever I need. Unfortunately, It doesn't work, and I don't really know if I can do it or if it's the right way to do it.

Any advices, answers will be much appreciate !

Should I use ember-cookie instead, and have ember-simple-auth handling authentification, and add additional cookie for my additional token ? (It will gives me more control over each cookie)

Aucun commentaire:

Enregistrer un commentaire