Skip to content

Instantly share code, notes, and snippets.

@alonextou
Created August 20, 2013 16:38
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 alonextou/6283925 to your computer and use it in GitHub Desktop.
Save alonextou/6283925 to your computer and use it in GitHub Desktop.
App.AuthController = Ember.Controller.extend({
// isGuest: true,
observeIsGuest: function() {
console.log('isGuest changed: ' + this.isGuest);
}.observes('isGuest'),
login: function() {
self.set('isGuest', false);
});
},
logout: function() {
self.set('isGuest', true);
},
checkLogin: function() {
var self = this;
var response = $.Deferred();
$.ajax({
url: '/users/me',
type: 'GET'
//contentType: 'application/json',
}).done(function(data) {
response.resolve(data);
console.log('done checking');
if (typeof data === 'undefined') {
self.set('isGuest', true);
} else {
self.set('isGuest', false);
}
}).fail(function(xhr, status, error) {
console.log(xhr.responseText);
response.reject();
});
return response.promise();
}
});
App.ApplicationRoute = Ember.Route.extend({
beforeModel: function(transition) {
this.controllerFor('auth').checkLogin().done(function(){
transition.retry();
});
}
});
App.GuestRoute = Ember.Route.extend({
redirect: function() {
if(!this.controllerFor('auth').get('isGuest')){
this.transitionTo('index');
}
}
});
App.AuthRoute = Ember.Route.extend({
redirect: function() {
if(this.controllerFor('auth').get('isGuest')){
this.transitionTo('index');
}
}
});
/* Guest Routes */
App.LoginRoute = App.GuestRoute.extend({
renderTemplate: function() {
this.render({ controller: 'auth' });
},
setupController: function(controller, model) {
this.controllerFor('auth').reset();
},
});
/* Auth Routes */
App.LogoutRoute = App.AuthRoute.extend({
setupController: function(controller, model) {
this.controllerFor('auth').logout();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment