Reputation: 6221
In an Ember.js application, you can use various hooks to check authorization. There are beforeModel, willTransition, redirect hooks on route. also the same events applies on Router.
I'm developing authorization infrastructure for a project. All the route's are written by developers. But an abstract-route (which is extended by all routes) is my code.
So what is the correct way to check authorization and abort/prevent transition?
Upvotes: 0
Views: 323
Reputation: 5991
There is a nice plugin for authorization, ember-simple-auth, no need to re-invent the wheel.
If you insist to develop your own solution, you will need to: 1) Modify an adapter for ember data (to include auth headers in all queries) 2) Implement error handling on application route (handle 401 error) 3) (optional) Create a mixin for routes to check authorization on client side. beforeModel is a good place for this check, I guess. If developer will need to implement their own beforeModel hook, they just need to put this line in the beginning: this._super(...arguments);
Upvotes: 1
Reputation: 3988
Use beforeModel
. Example from ember-simple-auth:
beforeModel(transition) {
if (!this.get('session.isAuthenticated')) {
transition.abort();
this.transitionTo(Configuration.authenticationRoute);
} else {
return this._super(...arguments);
}
}
As Gennady suggests, you should look into ember-simple-auth since it does exactly what you want.
Upvotes: 1