Skip to content

Instantly share code, notes, and snippets.

@marcoow
Last active December 18, 2015 14:19
Show Gist options
  • Save marcoow/5796505 to your computer and use it in GitHub Desktop.
Save marcoow/5796505 to your computer and use it in GitHub Desktop.
authentication manager that handles everything related to client authgentication
App.AuthManager = Ember.Object.extend({
init: function() {
this._super();
var authToken = $.cookie('auth_token');
var authAccountId = $.cookie('auth_account');
if (!Ember.isEmpty(authToken) && !Ember.isEmpty(authAccountId)) {
this.authenticate(authToken, authAccountId);
}
},
isAuthenticated: function() {
return !Ember.isEmpty(this.get('session.authToken')) && !Ember.isEmpty(this.get('session.account'));
},
authenticate: function(authToken, accountId) {
var account = App.Account.find(accountId);
this.set('session', App.Session.createRecord({
authToken: authToken,
account: account
}));
},
reset: function() {
this.set('session', null);
},
sessionObserver: function() {
App.Store.authToken = this.get('session.authToken');
if (Ember.isEmpty(this.get('session'))) {
$.removeCookie('auth_token');
$.removeCookie('auth_account');
} else {
$.cookie('auth_token', this.get('session.authToken'));
$.cookie('auth_account', this.get('session.account.id'));
}
}.observes('session')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment