Bobby Sciacchitano
Bobby Sciacchitano

Reputation: 851

ember.js + ember-simple-auth how can I load application models after authentication?

I'm adding ember-simple-auth to handle authentication for an application I'm building. Currently in the ApplicationRoute I'm using a model to load sidebar content.

Some of the data is dependant on a user URL property which is returned with the auth token.

I'm refactoring my code to handle loading data for the authenticated user but I'm unsure where to put the model call to load the sidebar data.

I'm thinking it'd make sense to add an observer on the isAuthenticated property to trigger the model load or take my current routes and wrap them in a resource which is responsible for loading the model?

Application Route

App.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin,
{
    model: function()
    {
        return Ember.RSVP.hash(
        {
            collections: Ember.$.getJSON(this.session.get('user.url') + '/collection'),

            libraries: Ember.$.getJSON(ENV.api + '/library')
        });
    },


    setupController: function(controller, model)
    {
        controller.set('libraries', model.libraries);

        controller.set('collections', model.collections);
    }
});

Route Mapping

App.Router.map(function()
{
    this.route('login');

    // Authenticated Routes

    this.route('my-account');

    this.route('collection', { path: '/collection/:id' });

    this.route('item.new', { path: '/item/new' });

    this.route('item.edit', { path: '/item/:id' });

    this.route('library', { path: '/:slug' });
});

Upvotes: 0

Views: 371

Answers (2)

Sam Selikoff
Sam Selikoff

Reputation: 12704

You can use this.get('session.user_email') in routes to get the authenticated user's email, and then use that to fetch the user data from the server.

Upvotes: 0

marcoow
marcoow

Reputation: 4062

The ApplicationRouteMixin defines the sessionAuthenticationSucceeded action that is invoked whenever the session state changes from not authenticated to authenticated. You can also listen to the session's sessionAuthenticationSucceeded event

Upvotes: 0

Related Questions