5

I'm using ember-simple-auth in my application and it's working well, but I would like to be able to display properties from the current user (such as email or name) in the UI. In the past I've used an application initializer to do this and essentially inject all controllers with the currentUser, but that required the current user to be known when the application was initialized. Since I'm using OAuth, the user is not known when the application loads.

Is there a way to get properties from the currently logged in user?

Peter Brown
  • 50,956
  • 18
  • 113
  • 146

1 Answers1

7

It turned out the version of ember-simple-auth I was using was out of date and needed to upgrade to 0.3.x (from 0.2.x). From there, I was able to add a custom authenticator which I pulled almost directly from the project's example files. Note that I'm on Ember 1.6.0 beta 2.

With the code below, I can access the currentUser in routes and controllers using this.get('session.currentUser') or in templates using {{session.currentUser}}.

The only change I had to make to my API was including the user_id with the OAuth response.

Updated from the previous answer to support 0.4.0

Then I updated my initializer to be the following:

App.initializer({
  name: 'authentication',

  initialize: function(container, application) {
    Ember.SimpleAuth.Authenticators.OAuth2.reopen({
      serverTokenEndpoint: '/api/oauth/token'
    });

    Ember.SimpleAuth.Session.reopen({
      currentUser: function() {
        var userId = this.get('user_id');
        if (!Ember.isEmpty(userId)) {
          return container.lookup('store:main').find('current-user', userId);
        }
      }.property('user_id')
    });

    Ember.SimpleAuth.setup(container, application, {
      authorizerFactory: 'ember-simple-auth-authorizer:oauth2-bearer',
      routeAfterAuthentication: 'main.dashboard'
    });
  }
});

My login controller now looks like this:

export default Ember.Controller.extend(Ember.SimpleAuth.LoginControllerMixin, {
  authenticatorFactory: 'ember-simple-auth-authenticator:oauth2-password-grant'
});
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • 1
    This is absolute gold! I've been struggling to find a good way to do this with ember-simple-auth for months. This should really be part of the docs (if not the standard implementation). – opsb Jun 01 '14 at 18:06
  • @opsb I just updated my answer because it can be simplified even more after a recent release of ember-simple-auth. The custom authenticator is no longer required. – Peter Brown Jun 01 '14 at 23:09
  • nice, that does look cleaner. – opsb Jun 02 '14 at 22:16
  • 2
    I found a subtle issue with returning a promise from currentUser, when doing comparison like session.get("currentUser") === task.get("user") it will fail (even once the promise has resolved). Perhaps I'm meant to be using an ember specific equality check? Because of this though I've switched to using an observer in the session which sets the currentUser when the user has loaded i.e. https://gist.github.com/opsb/d3754a4541a120c92cad – opsb Jun 03 '14 at 13:54
  • opsb Have you run into other issues with this? I'm starting to think setting the prop. on the session async is not good, because often the rest of the application 'expects' the currentUser to be defined to do stuff (like return related models in a route). – Sam Selikoff Jun 21 '14 at 06:23
  • @SamSelikoff I had some issues related to redirecting a user on login because the currentUser was not available on the session yet. I ended up just loading the model on the index route like this: https://gist.github.com/beerlington/f678c5faa88e093099f8 – Peter Brown Jun 21 '14 at 12:58