Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Last active August 29, 2015 14:25
Show Gist options
  • Save Lazhari/762436618eedb0bb34d3 to your computer and use it in GitHub Desktop.
Save Lazhari/762436618eedb0bb34d3 to your computer and use it in GitHub Desktop.
Authentication Angularjs Factory uses localStorage
'use strict';
angular.module('psJwtApp')
.factory('authToken', function ($window) {
var storage = $window.localStorage;
var cachedToken;
var userToken = 'userToken';
var authToken = {
setToken: function(token) {
cachedToken = token;
storage.setItem(userToken, token);
},
getToken: function() {
if(!cachedToken)
cachedToken= storage.getItem(userToken);
return cachedToken;
},
isAuthenticated: function() {
return !!authToken.getToken();
},
removeToken: function() {
cachedToken = null;
localStorage.removeItem(userToken);
}
};
return authToken;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment