auth0 / angular-storage Goto Github PK
View Code? Open in Web Editor NEWA storage library for AngularJS done right
License: MIT License
A storage library for AngularJS done right
License: MIT License
In my case, I have a lot of keys stored and on logout I just need remove all of then.
In angular-storage on line 86 a check is performed for localStorage enabled on the browser by
var localStorageAvailable = !!$window.localStorage;
This check is not adequate enough bc/ at least in chrome, you get an error:
Error: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
I recommend removing that line and rather just using the try catch that is performed below as:
try {
$window.localStorage.setItem('testKey', 'test');
$window.localStorage.removeItem('testKey');
var localStorageAvailable = true;
} catch(e) {
var localStorageAvailable = false;
}
This way chrome wouldn't break the script on the site.
This code executes before any other check in other app modules can mitigate this (for angularjs apps with localStorage turned off) This would be in order to even show a warning notice to the user.
Hi,
Thank you for this awesome storage package. I have been using this in many ways and in many projects.
Today while using store I found a bug. The issue is, first I set a JSON using store.set('objectName',myObject);.
And when I need it I fetch that object using store.get('objectName'), and once I get this object I set a new property in this object, but while doing this the store('objectName') gets updated from localstorage automatically with that new property.
Example:
angular
.controller('myCtrl', function(store){
var myObject = {
'value_1': 'value 1',
'value_2': 'value 2'
};
store.set('objectName', myObject);
})
.controller('newCtrl', function (){
var newObject = store.get('objectName');
newObject.value_3 = 'value 3';
console.log(store.get('objectName'));
});
above console.log gives you below result
{
'value_1': 'value 1',
'value_2': 'value 2',
'value_3': 'value 3'
}
Can anyone help me with this?
Looking at the code, JSON.stringify
with remove any functions or custom getters/setters attached to an object. I get the point of the library - to get an actual JS object back - but it seems like the tag line is a little misleading.
Could there be a serializer/deserializer that allows one to store and get back objects with functions?
Hey guys, it seems like Safari disables local/session storage when in private browsing mode. This causes angular apps to fail when trying to use the store methods. The error thrown:
Error: QuotaExceededError: DOM Exception 22
It would be nice to fall back to cookies in this instance or at least catch the error and do something with it.
bower a0-angular-storage#0.0.13 ENORESTARGET No tag found that was able to satisfy 0.0.13
Due to my project need to store some sensitive data, seems this library haven't encryption function. I have to encrypt, decrypt the data before save and get. So I want to ask, do you have any plan to add encryption function for easy use?
Thanks
Changes asked by @matjaz:
We're currently using this workaround:
store.clear = function() {
window.localStorage.clear();
};
But it would be awesome if this were supported in the library. Thanks!
I wanted to ask about the purpose of the internal storage caching. Is it due to the performance reasons?
I am using the store for keeping the jwt token.
I wanted to test the jwt token being tampered by manually altering the token in the browser's localStorage.
I was surprised that the subsequent request came from the server with 200 OK.
It was caused by the internally cached value in the store.
Isn't this default behavior a bit confusing? IMO it would be nice to have the caching functionality switchable via the provider configuration.
Is there a way to fetch all the data stored for a namespace or just in general without writing vanilla js?
Example:
store.get() <--- returns all items in storage.
store.namespace('xyz') <--- returns all item in storage for xyz
namespace.
Currently if you set store in config block like this:
storageProvider.setStore('sessionStorage')
Then if you create a namespaced storage it will use localStorage. I think instead it should use the storage type set in config block.
On a fresh clone, I get the following test output:
Start:
angularStorage
✔ should load store module
angularStorage store
✔ should save items correctly in localStorage
✔ should save null items correctly in localStorage
✔ should save undefined items correctly in localStorage
✔ should delete items correctly from localStorage
✔ should save objects correctly
✔ should save and objects correctly without cache
angularStorage storeProvider.setStore("sessionStorage")
✔ should save items correctly in the sessionStorage
✖ should fallback to cookieStorage
angularStorage storeProvider.setStore("localStorage")
✔ should save items correctly in the localStorage
angularStorage storeProvider.setStore("cookieStorage")
✔ should save items correctly in the cookieStorage
angularStorage storeProvider.setStore()
✔ should save items correctly in the localStorage
angularStorage storeProvider.setStore(123)
✔ should save items correctly in the localStorage
angularStorage storeProvider.setStore("abc")
✔ should throw an error when the store is not found
angularStorage store: cookie fallback
✖ should save items correctly in localStorage
✖ should save null items correctly in localStorage
✖ should save undefined items correctly in localStorage
✖ should delete items correctly from localStorage
✖ should save objects correctly
✖ should save objects correctly without cache
angularStorage new namespaced store
✔ should save items correctly
✔ should delete items correctly from localStorage
✔ should save objects correctly
✔ should save and objects correctly without cache
✔ should should save items correctly when the delimiter is set
with param storage
✔ should should save items correctly when the storage is set to sessionStorage
✔ should should save items correctly when the storage is set to localStorage
✔ should should save items correctly when the storage is set to cookieStorage
Finished in 0.007 secs / 0.027 secs
SUMMARY:
✔ 25 tests completed
✖ 9 tests failed
FAILED TESTS:
angularStorage storeProvider.setStore("sessionStorage")
✖ should fallback to cookieStorage
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:119
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
angularStorage store: cookie fallback
✖ should save items correctly in localStorage
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:254
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
✖ should save null items correctly in localStorage
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:260
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
✖ should save undefined items correctly in localStorage
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:267
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
✖ should delete items correctly from localStorage
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:275
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
✖ should save objects correctly
PhantomJS 1.9.8 (Mac OS X 0.0.0)
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:294
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
✖ should save objects correctly without cache
PhantomJS 1.9.8 (Mac OS X 0.0.0)
PhantomJS 1.9.8 (Mac OS X 0.0.0)
TypeError: 'undefined' is not an object (evaluating 'rawDocument.cookie = buildCookieString(name, value, options)')
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:309
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:143
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:159
at /Users/duncan/working/oss/angular-storage/bower/angular-cookies/angular-cookies.js:241
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/cookieStorage.js:6
at /Users/duncan/working/oss/angular-storage/src/angularStorage/services/internalStore.js:21
at /Users/duncan/working/oss/angular-storage/test/unit/angularStorage/services/storeSpec.js:313
at invoke (/Users/duncan/working/oss/angular-storage/bower/angular/angular.js:4426)
at workFn (/Users/duncan/working/oss/angular-storage/bower/angular-mocks/angular-mocks.js:2420)
undefined
[15:13:44] 'test' errored after 1.73 s
[15:13:44] Error: 1
at formatError (/.npm-packages/lib/node_modules/gulp/bin/gulp.js:169:10)
at Gulp.<anonymous> (/.npm-packages/lib/node_modules/gulp/bin/gulp.js:195:15)
at Gulp.emit (events.js:107:17)
at Gulp.Orchestrator._emitTaskDone (/Users/duncan/working/oss/angular-storage/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
at /Users/duncan/working/oss/angular-storage/node_modules/gulp/node_modules/orchestrator/index.js:275:23
at finish (/Users/duncan/working/oss/angular-storage/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
at cb (/Users/duncan/working/oss/angular-storage/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
at removeAllListeners (/Users/duncan/working/oss/angular-storage/node_modules/karma/lib/server.js:218:7)
at Server.<anonymous> (/Users/duncan/working/oss/angular-storage/node_modules/karma/lib/server.js:229:9)
at Server.g (events.js:199:16)
at Server.emit (events.js:129:20)
at net.js:1409:10
at process._tickCallback (node.js:355:11)
store.set('jwt', response.data.token);
This stores the token value encapsulated in quotes. Is there any way to avoid this? I just want the plain output from response.data.token
which is unquoted.
According to the README, the Bower package name is angular-storage
. However, this name is already taken and resolves to another package (http://github.com/Aspera/angular-storage.git).
Do you have a timeline for releasing a new version of the bower package, incorporating the sessionStorage changes in master?
Hi, I'm having this error
Error: [$injector:nomod] Module 'ngCookies' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
I even tried to install ngCookies
. but it still didn't work. Anyone else having this problem?
bower install a0-angular-storage
bower not-cached git://github.com/Aspera/angular-storage.git#0.0.5
bower resolve git://github.com/Aspera/angular-storage.git#0.0.5
bower cached git://github.com/auth0/angular-storage.git#0.0.10
bower validate 0.0.10 against git://github.com/auth0/angular-storage.git#*
bower ENORESTARGET No tag found that was able to satisfy 0.0.5
Additional error details:
Available versions: 0.5.0, 0.4.0, 0.3.0, 0.2.0, 0.1.2, 0.1.1, 0.1.0
Hi, Can we improve the set() function to accept either a key-value pair or an array of key-value pair?
Example:
// For key-value pair
storage.set('key1', 'Value1');
// For multiple key-value pairs
storage.set([
'my-key1': 'My Value 1',
'my-key2': { 'key1': 'value' }
]);
I think this will be a good addition.
It would be nice to be able to declare a namespace, e.g. store.namespace("foo"). The namespace would then be prepended to all keys, e.g. store.get("key") would be equivalent in this case to store.get("foo-key") if you'd never declared a namespace.
There should also be an option to specify a different namespace on a per-get basis, or even specify that no namespace should be used.
store.get should allow (or even require?) a callback function to be passed, in anticipation of supporting storage methods which require asynchronous retrieval.
Hi,
From what I see ( your examples! ) - this module is pretty much exactly the same as Angular's $cacheFactory
. ( https://docs.angularjs.org/api/ng/service/$cacheFactory )
How is this different from $cacheFactory
? Or why would one want to use angular-storage
as opposed to $cacheFactory
.
Can I use both sessionStorage and localStorage at the same time? For example if user checks the 'Remind me' button, I would want to use localStorage, but I don't know that before…
When I store a date to the local storage, after a refresh when I get
it, it becomes a string.
Note that when I store it and I get it immediately after (Without the refresh) it is an object.
Sample code below. Run this twice and see the difference. The first time its run the gotten date will be an object, Refresh the page and have it run again (so that the value already exists) and it will be a string.
var someDateToGet = store.get('someStoredDate');
console.debug(someDateToGet);
if (someDateToGet === null){
var someDateToStore = new Date();
console.debug("someDateToStore: " + typeof(someDateToStore));
//returns someDateToStore: Object
store.set('someStoredDate', someDateToStore);
//To check, that went well:
someDateToGet = store.get('someStoredDate');
console.debug("someDateToGet: " + typeof(someDateToGet));
//returns someDateToGet: Object
} else {
//The date was already stored.
console.debug("someDateToGet: " + typeof(someDateToGet));
//returns someDateToGet: string
}
The expected behaviour would for it to become an object regardless of the refresh. Accepted behaviour would for it to become the same thing (either an object or a string) every time it's gotten from local storage.
This library works with 1.3.x, so can you please update bower.json?
Getting this:
a0-angular-storage#0.0.6, angular-jwt#0.0.4 depends on angular#~1.2.21 which resolved to angular#1.2.28
The source code of get()
has the following check
if (typeof saved === 'undefined' || saved === 'undefined') {
obj = undefined;
} else {
obj = JSON.parse(saved);
}
Actually getItem(key)
returns null
. See https://www.w3.org/TR/webstorage/#dom-storage-getitem
How do I access the storage in a .config function?
I had a look through your repositories for an update to this package as we update an AngularJS application to Angular. Is there an updated Angular version of this package?
I really hate it when libs use hard dependencies, esp when your not updating them weekly...
"dependencies": {
"angular": "~1.2.21"
},
"devDependencies": {
"angular-mocks": "~1.2.21",
"angular-scenario": "~1.2.21"
}
Please update this to be >=1.2 or >=1.3...
First of all. Good job on creating this library.
I do have one suggestion.
While using the library I was unable to fetch a String based cookie from my list of cookies.
The library wanted to have an cookie that was JSON based, but because my Sting based cookie was not set by using the library (set from a response header), it failed.
It seems the $cookieStorage is using Angular's $cookieStore.
According to the Angular documentation this is a deprecated service.
https://docs.angularjs.org/api/ngCookies/service/$cookieStore
You should use the $cookies service instead.
Please consider changing services to support the use of non JSON bases cookies.
Thanks in advance.
Hi auth0 team,
great work so far. Are there any plans to also support sessionStorage as a store. Ideally the user can configure which store to use, but the fallback is always the sessionStorage. I am open to add a pull request with the feature.
Greetings
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.