Created
July 28, 2015 14:34
-
-
Save eliotsykes/2bc1cbaadc1e49dfad66 to your computer and use it in GitHub Desktop.
Ember Simple Auth Configurable Authentication Route Mixin
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
// 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