Created
May 12, 2016 00:52
-
-
Save cassioscofield/c6384e19ba2fd556193c007b1efa4c58 to your computer and use it in GitHub Desktop.
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
/*global angular*/ | |
angular.module('provider.keystore', [ | |
]) | |
.provider('Keystore', function () { | |
var retrieveKey, saveKey, removeKey, clearData, $cookies; | |
if (typeof window.localStorage !== 'undefined' && window.localStorage) { | |
console.log('initialized local storage'); | |
removeKey = function (key) { | |
localStorage.removeItem(key); | |
}; | |
clearData = function () { | |
localStorage.clear(); | |
}; | |
retrieveKey = function (key, default_value) { | |
var value = window.localStorage.getItem(key); | |
if (value) { | |
return value; | |
} | |
return default_value; | |
}; | |
saveKey = function (key, value) { | |
window.localStorage.setItem(key, value); | |
}; | |
} else { | |
console.log('initialized cookie storage'); | |
removeKey = function (key) { | |
$cookies.remove(key); | |
}; | |
clearData = function () { | |
var cookies = $cookies.getAll(); | |
angular.forEach(cookies, function (v, k) { | |
$cookies.remove(k); | |
}); | |
}; | |
retrieveKey = function (key, default_value) { | |
var value = $cookies.get(key); | |
if (value) { | |
return value; | |
} | |
return default_value; | |
}; | |
saveKey = function (key, value) { | |
$cookies.put(key, value); | |
}; | |
} | |
return { | |
setup: function (cookies) { | |
this.$cookies = cookies; | |
}, | |
retrieveKey: retrieveKey, | |
saveKey: saveKey, | |
clearData: clearData, | |
$get: function () { | |
return { | |
retrieveKey: retrieveKey, | |
saveKey: saveKey, | |
removeKey: removeKey, | |
clearData: clearData | |
}; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment