Last active
August 29, 2015 14:25
-
-
Save Lazhari/762436618eedb0bb34d3 to your computer and use it in GitHub Desktop.
Authentication Angularjs Factory uses localStorage
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
'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