Search code examples
sessionauthenticationember.jsember-simple-auth

Get a hold of Ember Simple Auth session via container


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.


Solution

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