Skip to content

Instantly share code, notes, and snippets.

@th3hamburgler
Created September 21, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save th3hamburgler/10c8930fa6f866ef82fc24af2cce7db9 to your computer and use it in GitHub Desktop.
Save th3hamburgler/10c8930fa6f866ef82fc24af2cce7db9 to your computer and use it in GitHub Desktop.
Ember Auth Route
import Ember from 'ember';
const {
Mixin
} = Ember;
/*
* This mixin will prevent an authorised user from
* accessing any route that does not allow their role
*
* Usage:
* - Add the mixin to your route
* - Define "allowedRoles" attribute array and add any roles
* that can access the route
*/
export default Mixin.create({
allowedRoles: [],
beforeModel: function() {
this._super();
this.userCanAccessRoute();
},
activate: function() {
this._super();
this.userCanAccessRoute();
},
userCanAccessRoute: function() {
let role = this.session.get('currentUser.role'),
allowedRoles = this.get('allowedRoles');
if (allowedRoles.indexOf(role) === -1) {
return this.transitionTo('internal.dashboard');
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment