Created
April 7, 2014 22:37
-
-
Save hbrysiewicz/10069664 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
ItemManager.AuthenticatedRoute = Ember.Route.extend({ | |
beforeModel: function(transition) { | |
if (!this.controllerFor('sign_in').get('user')) { | |
this.redirectToSignIn(transition); | |
} | |
}, | |
redirectToSignIn: function(transition) { | |
var signInController= this.controllerFor('sign_in'); | |
signInController.set('attemptedTransition', transition); | |
this.transitionTo('sign_in'); | |
}, | |
actions: { | |
error: function(reason, transition) { | |
if (reason.status == 401) { | |
this.redirectToSignIn(transition); | |
} else { | |
alert('Something went wrong'); | |
} | |
} | |
} | |
}); | |
ItemManager.SignInRoute = Ember.Route.extend({ | |
setupController: function(controller) { | |
controller.reset(); | |
} | |
}); | |
ItemManager.SignUpRoute = Ember.Route.extend({ | |
setupController: function(controller) { | |
controller.reset(); | |
} | |
}) |
This file contains hidden or 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
ItemManager.SignInController = Ember.Controller.extend({ | |
needs: ['application'], | |
showDropdown: Ember.computed.alias('controllers.application.showDropdown'), | |
user: function(key, val) { | |
if (arguments.length > 1) { | |
localStorage.setItem('user', JSON.stringify(val)); | |
return val; | |
} | |
var rawUser = localStorage.getItem('user'); | |
if(typeof rawUser === null){ | |
return null; | |
} else { | |
return JSON.parse(rawUser); | |
} | |
}.property(), | |
actions: { | |
login: function() { | |
var _this = this, | |
data = {user: this.getProperties('email', 'password')}; | |
_this.set('errorMessage', null); | |
$.post('/users/sign_in', data) | |
.success(function(response) { | |
_this.set('user', response.user_session); | |
var attemptedTransition = _this.get('attemptedTransition'); | |
if (attemptedTransition) { | |
attemptedTransition.retry(); | |
_this.set('attemptedTransition', null); | |
} else { | |
_this.transitionToRoute('projects.index'); | |
} | |
}).error(function(response) { | |
_this.set('errorMessage', response.responseText); | |
}); | |
}, | |
logout: function(){ | |
// TODO: This should issue a `DELETE /users/sign_out` request to the server | |
this.setProperties({ | |
user: null, | |
showDropdown: false | |
}); | |
delete localStorage.user; | |
this.transitionToRoute('sign_in'); | |
} | |
}, | |
reset: function() { | |
this.setProperties({ | |
email: "", | |
password: "", | |
errorMessage: "" | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How are you actually using AuthenticatedRoute? I mean, how do you actually use it in your router.js?