Created
November 28, 2014 13:26
-
-
Save pk/ed17387907c12cdfece3 to your computer and use it in GitHub Desktop.
Angular AuthSession model
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
/** | |
* 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