Search code examples
ember.jsember-simple-auth

ember.js: Make AdminRouteMixin from AuthenticatedRouteMixin?


Is there a more elegant way to prevent unauthorized access to an admin-only route than writing this in all of my admin routes?

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  beforeModel: function(){
    if(!this.get('session.secure.admin')) this.transitionTo("dashboard");
  }
});

Perhaps it's possible to extend AuthenticatedRouteMixin itself to make this kind of check? Thanks!


Solution

  • Why not just make the mixin?

    import Ember from 'ember';
    import AuthenticatedRouteMixin from 'wherever/it/is'.
    
    const { Mixin } = Ember;
    
    export default Mixin.create(AuthenticatedRouteMixin, {
      beforeModel(){
        if(!this.get('session.secure.admin')) {
          this.transitionTo("dashboard");
        }
      }
    })
    

    And then import it in your routes:

    import Ember from 'ember';
    import AdminCheckMixin from 'yourApp/mixins/routes/admin-check';
    
    const { Route } = Ember;
    
    export default Route.extend(AdminCheckMixin);