1
votes

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.

1
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
I'm attempting to follow this gist, but I changed after: authentication to after: 'simple-auth-devise', which I think is correct.niftygrifty
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: github.com/simplabs/ember-simple-auth/blob/master/examples/…marcoow
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
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

1 Answers

2
votes

@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')
)