Reputation: 3029
I'm currently testing on my local machine so the URL is as following when the user presses Sign out
// The ********** is my site name, which i don't want releasing yet
http://localhost:8080/**********/logout/
Local storage item:
Key: tkn Value: EE8D9DA0F1D71A0BE8B80F3BA80343555655871151tk16
the following tests i've tried:
if (localStorage.getItem('tkn')) {
localStorage.clear();
}
if (localStorage.getItem('tkn')) {
localStorage.removeItem('tkn');
}
// I've also ran these both tests in the console of the browser
// and they return back 'undefined' but the item is cleared.
However when I sign out
, the user session is removed completely fine. However the localStorage doesn't seem to be cleared.
Could someone shred some light on this for me?
Upvotes: 1
Views: 5359
Reputation: 68393
Instead of checking a specific property, check the length
if (localStorage.length > 0 ) {
localStorage.clear();
}
localStorage is an implementation of Storage interface and Storage has a property called length
Storage.length
Returns an integer representing the number of data items stored in the Storage object.
Upvotes: 3