Skip to content

Instantly share code, notes, and snippets.

@pk
Created November 28, 2014 13:26
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 pk/ed17387907c12cdfece3 to your computer and use it in GitHub Desktop.
Save pk/ed17387907c12cdfece3 to your computer and use it in GitHub Desktop.
Angular AuthSession model
/**
* Authentication session handling token and user id
*/
angular.module('fn.services.authentication', [])
.service('srvAuthenticationSession', ['$window', function ($window) {
this.save = function() {
var data = {'userId':this.userId, 'token':this.token};
$window.localStorage['com.fansnation.authorisationSession'] = JSON.stringify(data);
}
this.load = function() {
var session = JSON.parse($window.localStorage['com.fansnation.authorisationSession']) || {};
this.userId = session['userId'] || null;
this.token = session['token'] || null;
}
this.create = function (userId, token) {
this.userId = userId;
this.token = token;
this.save();
};
this.destroy = function () {
this.userId = null;
this.token = null;
this.save();
};
this.isValid = function() {
return !!this.token && !!this.userId;
};
return this;
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment