Created
September 21, 2017 16:29
-
-
Save th3hamburgler/10c8930fa6f866ef82fc24af2cce7db9 to your computer and use it in GitHub Desktop.
Ember Auth Route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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