Search code examples
ember.jsember-simple-auth

ember-simple-auth: Any way to get session from within the Router?


I want to use analytics tracking on every transition like mentioned in Mixpanel with EmberJS

In order to do so, I need to be able to reopen the Router.

Is there any way with ember-simple-auth to get the current session there? My understanding was that it's available to all the routes and controllers, but saw no mention of the Router specifically.

EDIT:

An alternative approach I'm exploring right now is to include a mixin on all the routes where I want to do analytics identification. I have a mixin like the following:

`import Ember from 'ember'`

AnalyticsMixin = Ember.Mixin.create
  beforeModel: (transition) ->
    @_super(transition)
    userId = @get('session.user_id')
    if (!Ember.isEmpty(userId))
      user = @store.find('user', userId)
      username = user.get('username') # this doesn't work

I can get the user_id from the session object, although the Session.reopen that I did doesn't seem to include the user on its own. Nor does @store.find('user', userId) work.

The following works fine in a template:

Authentication =
  name: "authentication"
  before: "simple-auth"
  initialize: (container) ->
    Session.reopen
      user: (->
        userId = @get('user_id')
        if (!Ember.isEmpty(userId))
          return container.lookup('store:main').find('user', userId)
      ).property('userId')
    container.register("authenticator:custom", CustomAuthenticator)

Solution

  • You can always get the session from Ember's container with

    container.lookup('simple-auth-session:main');