Best practice for storing authenticated user's data


#1

Hello,

I’m looking for the best way to store my authenticated user’s informations. Basically is an Ember.Object instance (could be an Ember.ObjectController instance as well) that saves basic user’s informations (id, name, email, etc). This object has to be reachable from all controllers and routes (to deal with access level and to display user specific informations for example). Nowadays, I store this object in the App global (something like App.user). I would like to know what’s your solution and if there is better way to do that. Especially, I’m asking myself if it would be better to store the user in the App._container_ to “legally” access the user from controllers and routes (this.container.user).


#2

Maybe the application controller would be the place for this:

App.ApplicationController = Ember.Controller.extend({
  user: // user data goes here
});

App.OtherController = Ember.Controller.extend({
  needs: ['application'],
  // use it with:
  userBinding: 'controllers.application.user',
  // or with:
  userProperty: function() {
    return this.get('controllers.application.user');
  }.property('controllers.application.user'),
  // etc...
});

#3

In our apps we have a Session object that we inject into all controllers and routes, something like this:

App.initializer({
  name: 'session',
  initialize: function(container, application) {
    container.register('app:session', App.Session);

    container.injection('controller', 'session', 'app:session');
    container.injection('route', 'session', 'app:session');
  }

#4

The offical api is container.injection not container.typeInjection. Check the docs


#5

Thanks for the information (I’ve updated the code in the example accordingly). I just copied and pasted from an old app that was written before the container was documented. Regardless of API, I still think that using injection is a clean solution to the stated problem, and was really looking for feedback as to whether or not there was a better solution.