Skip to content

Instantly share code, notes, and snippets.

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 eliotsykes/2bc1cbaadc1e49dfad66 to your computer and use it in GitHub Desktop.
Save eliotsykes/2bc1cbaadc1e49dfad66 to your computer and use it in GitHub Desktop.
Ember Simple Auth Configurable Authentication Route Mixin
// For use with the Ember Simple Auth Addon. This is one way to default to
// using authentication required routes. For a preferred solution see:
// https://github.com/simplabs/ember-simple-auth/wiki/Recipe:-Defaulting-to-Authentication-Required-Routes
import Ember from 'ember';
import AuthConfig from 'simple-auth/configuration';
export default Ember.Mixin.create({
// The more secure default is to need authentication for all routes. Do not
// change this value in this file. Only change needsAuthentication in the
// route.js file for the route that does not need authentication.
needsAuthentication: true,
getSession: function() {
return this.get(AuthConfig.sessionPropertyName);
},
demandAuthentication: function(transition) {
transition.abort();
this.getSession().set('attemptedTransition', transition);
Ember.assert(
'Infinite transition loop detected. AuthConfig.authenticationRoute should not demand authentication.',
this.get('routeName') !== AuthConfig.authenticationRoute
);
transition.send('sessionRequiresAuthentication');
},
demandAuthenticationIfRequired: function(transition) {
var isRouteWithURL = this.routeName !== 'application';
if (isRouteWithURL && this.get('needsAuthentication') && !this.getSession().get('isAuthenticated')) {
this.demandAuthentication(transition);
}
},
beforeModel: function(transition) {
var superResult = this._super(transition);
this.demandAuthenticationIfRequired(transition);
return superResult;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment