From 19f276686ffed73fe683696d581fff1ff3d1b1c4 Mon Sep 17 00:00:00 2001 From: Saheb Giri Date: Tue, 2 Jul 2024 18:01:13 +0530 Subject: [PATCH] fix: user detail persists in ls after logout --- packages/web-storage/package.json | 3 ++- packages/web-storage/src/index.ts | 23 ++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/web-storage/package.json b/packages/web-storage/package.json index 2526cfcb..a008761b 100644 --- a/packages/web-storage/package.json +++ b/packages/web-storage/package.json @@ -1,6 +1,6 @@ { "name": "@groww-tech/web-storage", - "version": "0.0.4", + "version": "0.0.5", "description": "Web Storage is a service used that exposes methods to get full control over storage mechanisms available via bucketing.", "main": "dist/index.js", "module": "dist/esm/index.js", @@ -15,6 +15,7 @@ }, "scripts": { "build": "tsup --env.NODE_ENV production", + "watch": "tsup --watch --env.NODE_ENV production", "test" : "vitest run", "pushTags": "PACKAGE_VERSION=$(cat package.json | grep \\\"version\\\" | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag -a web-storage-v$PACKAGE_VERSION -m \"@groww-tech/web-storage-v$PACKAGE_VERSION\" && git push --tags" }, diff --git a/packages/web-storage/src/index.ts b/packages/web-storage/src/index.ts index 1abbb473..2c47f928 100644 --- a/packages/web-storage/src/index.ts +++ b/packages/web-storage/src/index.ts @@ -20,7 +20,6 @@ import { getUserProvidedKeyFromStoredKey } from './helpers'; - /** * Get Data from Storage * @@ -215,7 +214,6 @@ export function clearStorage(storageType: string) { } } - /** * Clears the Local Storage based on the original exported method i.e; clearStorage(); * @@ -286,27 +284,26 @@ function clearStorageCookies() { */ export function clearBucketStorage(bucket: string) { - const localStorageLength = localStorage.length; + const keysToRemove = []; - // PLEASE NOTE:- - // - // we are keeping reverse array because everytime if() is true, the original array gets altered - // and the keys move 1 index up. For example:- If LS length is initial 10 and iterator is at 0. - // If we remove the first key, LS length becomes 9 and iterator will be at 1. The key that will be - // initial at index 1 will be moving to index 0 now. And since the iterator is at 1, index 0 will not be deleted. - // this is why we are keeping the loop backwards startin from length - 1; - // - for (let index = localStorageLength - 1; index >= 0; index--) { + for (let index = 0; index < localStorage.length; index++) { const key = localStorage.key(index) || ''; + const userKey = getUserProvidedKeyFromStoredKey(key); // we want to extract bucket name from original stored key. // and remove the keys only that stores values not expiration time. // they will automatically be cleared after the original key is removed. if (getBucketNameFromKey(key) === bucket && !userKey.includes('-exptime')) { - clearKeyFromStorage(userKey, STORAGE_TYPE.LOCAL_STORAGE, bucket); + keysToRemove.push(userKey); } } + + // we are removing the keys in a separate loop because if we remove the keys in the above loop + // the length of the localStorage will change and the index may be altered. + keysToRemove.forEach((userKey) => { + clearKeyFromStorage(userKey, STORAGE_TYPE.LOCAL_STORAGE, bucket); + }); } /**