mercredi 20 avril 2016

Best way to get Current User session in Ember with Torii

I would like to know what the best way is to get the User Model Record of the current logged in user. I'm using torii in combination with corresponding firebase adapter.

app/torri-adapters/application.js

import Ember from 'ember';
import ToriiFirebaseAdapter from 'emberfire/torii-adapters/firebase';

export default ToriiFirebaseAdapter.extend({
  firebase: Ember.inject.service()
});

For example: I have a client model and at this moment i have this very ugly solution to query all client records associated with the current logged in user:

app/routes/clients.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    var _this = this;
    return this.store.query('user', {
      orderBy: 'email',
      equalTo: this.get('session.currentUser.email')
    }).then(function(user) {
      var myUser = user.get('firstObject');
      return _this.store.query('client', {
        orderBy: 'user',
        equalTo: myUser.id
      }).then(function(client) {
        return client;
      });
    });
  }
});

For sure there is a better way to do this?




Aucun commentaire:

Enregistrer un commentaire