3

I've been trying to create a session.currentUser property with an id, email, and points properties.

I'm referencing Custom authenticator with Ember simple auth + Ember CLI and How to store the user in a session, but I just can't figure out how the pieces fit together.

I'm using Ember CLI. In my index.html, I have:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:custom'
};

In initializers/custom-session.js, I have:

import Session from 'simple-auth/session';
import Devise from 'simple-auth-devise/authenticators/devise';

export default {
  name: 'session:custom',
  before: 'simple-auth',
  initialize: function(container) {
    container.register('session:custom', Devise);
    Session.extend({
      currentUser: function() {
        var id = this.get('id');
        var email: this.get('user_email');
        var points: this.get('points');

        if (!Ember.isEmpty(id)) {
          return this.store.createRecord('user', {
            id: id,
            email: email,
            points: points
          });
        }
      }.property('id')
    });
  }
};

This feels wrong to me in many ways, but after several hours of trying to make this work, it's at least an indication of what I'm trying to accomplish.

I'd be super grateful for any help. Thank you in advance!

Community
  • 1
  • 1
michael
  • 2,977
  • 3
  • 20
  • 26

1 Answers1

3

I got it!!! :)

index.html:

window.ENV['simple-auth'] = {
  authorizer: 'simple-auth-authorizer:devise',
  session: 'session:withCurrentUser'
};

initializers/customize-session.js:

import Session from 'simple-auth/session';

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

export default {
  name: 'customize-session',
  initialize: function(container) {
    container.register('session:withCurrentUser', SessionWithCurrentUser);
  }
};

Hope this helps someone else.

michael
  • 2,977
  • 3
  • 20
  • 26
  • My solution looks almost identical to yours. The only difference is (if you're using ember-cli) is I put the configuration of the custom session in `config/environment.js`. – Feech Aug 14 '14 at 18:51
  • Thanks for posting this! Can you tell me whether you explicitely defined userId in your model or if that is just the regular id that automatically gets pulled from the api? I have a rails devise api and it doesn't seem to pull the user with just this code, did you have anything fancy with the controller, route or did you have it just as described in the simple auth devise docs? – Coherent Feb 25 '15 at 05:01
  • 1
    @Coherent It's been a while and I don't have access to that code any more, but I believe that `this.get('user_id')` is the way you get the ID from Simple Auth. Then you use that to find the `user` by its regular old `id` property. I don't think there was any other configuration. – michael Feb 25 '15 at 16:04
  • Thanks, I appreciate the response. I know you don't have access to it, but do you happen to know what the rails routing looked like? Did you just say `devise_for :users, controllers: { sessions: 'sessions' }` and then on the next line `resources :users`? – Coherent Feb 28 '15 at 16:53
  • @Coherent I believe so. I actually created a PR on the [Ember Simple Auth Devise README](https://github.com/simplabs/ember-simple-auth/tree/master/packages/ember-simple-auth-devise) to flesh out the directions based on my experience, so if you follow the README, you should get a setup pretty similar to mine. – michael Mar 02 '15 at 00:28