Last active
August 29, 2015 14:06
-
-
Save nwlucas/ab18fdcbad3f557d31d9 to your computer and use it in GitHub Desktop.
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
(function() { | |
'use strict'; | |
ToDonts.ApplicationRoute = Ember.Route.extend(Ember.SimpleAuth.ApplicationRouteMixin, { | |
beforeModel: function(transition) { | |
var currentUser = this.get('currentUser'); | |
currentUser.reload(); | |
} | |
}); | |
}()); |
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
(function() { | |
'use strict'; | |
ToDonts.CurrentUser = Ember.ObjectProxy.extend(Ember.Evented, { | |
content: {}, | |
init: function() { | |
var self = this; | |
self.bindToSessionEvents(); | |
}, | |
reload: function() { | |
var self = this; | |
var session = this.container.lookup('ember-simple-auth:session:current'); | |
var authenticated = session.get('isAuthenticated'); | |
if(authenticated) { | |
Ember.$.getJSON('/api/users/me', function(data) { | |
self.setProperties({ content: data.current_user }); | |
}); | |
this.trigger('todonts:current_user_reloaded'); | |
} | |
}, | |
clear: function() { | |
this.setProperties({ content: {} }); | |
this.trigger('todonts:current_user_cleared'); | |
}, | |
bindToSessionEvents: function() { | |
var self = this; | |
var session = this.container.lookup('ember-simple-auth:session:current'); | |
session.off('ember-simple-auth:session-authentication-succeeded'); | |
session.on('ember-simple-auth:session-authentication-succeeded', function(content) { | |
console.log('Reloading current user: session authentication succeeded'); | |
self.reload(); | |
}); | |
session.off('ember-simple-auth:session-authentication-failed'); | |
session.on('ember-simple-auth:session-authentication-failed', function(content) { | |
console.log('Clearing current user: session authentication failed'); | |
self.clear(); | |
}); | |
session.off('ember-simple-auth:session-invalidation-succeeded'); | |
session.on('ember-simple-auth:session-invalidation-succeeded', function(content) { | |
console.log('Clearing current user: session invalidation succeeded'); | |
self.clear(); | |
}); | |
session.off('ember-simple-auth:session-invalidation-failed'); | |
session.on('ember-simple-auth:session-invalidation-failed', function(content) { | |
console.log('Clearing current user: session invalidation failed'); | |
self.clear(); | |
}); | |
} | |
}); | |
}()); |
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
//= require ../../lib/current_user | |
(function() { | |
'use strict'; | |
ToDonts.initializer({ | |
name: 'currentUser', | |
after: 'authentication', | |
initialize: function(container, application) { | |
var currentUser = ToDonts.CurrentUser.create({ container: container }); | |
container.register('todonts:current_user', currentUser, { instantiate: false }); | |
Ember.A(['model', 'controller', 'view', 'route']).forEach(function(component) { | |
container.injection(component, 'currentUser', 'todonts:current_user'); | |
}); | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment