Best practice for storing authenticated user's data


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).

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');
  // etc...

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

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

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

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

1 Like

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.