Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Created November 26, 2015 16:17
Show Gist options
  • Select an option

  • Save codeincontext/286e3cf5eca3c9a2e222 to your computer and use it in GitHub Desktop.

Select an option

Save codeincontext/286e3cf5eca3c9a2e222 to your computer and use it in GitHub Desktop.
An ngCordova mock for the keychain that persists values with localstorage
/**
* Overriding the keychain ngCordovaMock to use localStorage
**/
angular.module('ngCordovaMocks')
.factory('$cordovaKeychain', ['$q', function ($q) {
console.log('persistent keychain mock loaded')
return {
getForKey: function (key, serviceName) {
// note: this implementation doesn't fail on an invalid servicename
var key = 'keychain:' + serviceName + ':' + key;
return $q.when(localStorage[key]);
},
setForKey: function (key, serviceName, value) {
var key = 'keychain:' + serviceName + ':' + key;
localStorage[key] = value;
return $q.when();
},
removeForKey: function (key, serviceName) {
var key = 'keychain:' + serviceName + ':' + key;
delete localStorage[key];
return $q.when();
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment