1

Following this SO answer, I'm trying to get the Ember Simple Auth session with container.lookup('simple-auth-session:main'); but this gives me undefined.

I'm using Ember Cli and the Ember Simple Auth Devise authenticator.

Community
  • 1
  • 1
niftygrifty
  • 3,452
  • 2
  • 28
  • 49
  • is your initializer in that you're trying to get the session running after the `'simple-auth'` initializer? If not the session won't be already registered. – marcoow Aug 15 '14 at 06:03
  • I'm attempting to follow [this gist](https://gist.github.com/tabolario/9861932), but I changed `after: authentication` to `after: 'simple-auth-devise'`, which I *think* is correct. – niftygrifty Aug 15 '14 at 06:11
  • What are you actually trying to achieve? If it's creating a `currentUser` property somehow I'd suggest you follow this Ember Simple Auth example: https://github.com/simplabs/ember-simple-auth/blob/master/examples/4-authenticated-account.html – marcoow Aug 15 '14 at 06:30
  • I just had a look at that actually. I want to get a hold of an instance of the user like mentioned in that example, but I also want to inject the user into every controller and view. – niftygrifty Aug 15 '14 at 06:59
  • 1
    If you have a property for the user on the session object you don't need to inject the user into the controllers as well because the session is already injected and you can simply access the user via `session.user`. – marcoow Aug 15 '14 at 07:58

1 Answers1

2

@marcoow's comment above worked like a charm. He pointed me to this example

FIRST: add an initializer registering the custom session

Ember.Application.initializer(
  name: 'authentication'
  before: 'simple-auth'
  initialize: (container, application) ->
    container.register('session:custom', App.CustomSession)
)

SECOND: replace the session with your custom session

window.ENV['simple-auth'] = {
  session: 'session:custom'
}

THIRD: define the custom session

App.CustomSession = SimpleAuth.Session.extends(
  currentUser: (->
    unless Ember.isEmpty(@user_id)
      @container.lookup('session:main').load('user', @user_id)
      //Note! I'm using Ember Persistence Foundation, therefore 'session:main', 
      //but if you're using Ember Data, use 'store:main'
  ).property('user_id')
)
niftygrifty
  • 3,452
  • 2
  • 28
  • 49
  • Please note that `container.lookup('simple-auth-session:main');` will still give the default session. To look up for the custom session, you need to use `container.lookup('session:custom:main');` – Tushar Patel Sep 23 '14 at 08:48