简体   繁体   中英

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' });
});

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

您可以在路由中使用this.get('session.user_email')来获取经过身份验证的用户的电子邮件,然后使用该电子邮件从服务器中获取用户数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM