Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ const getLocalKey = (key: string, value: string | object | number | boolean | nu
};

/**
* This version of local storage supports the following data types as it is and other data types will be treated as string
* This version of storage supports the following data types as it is and other data types will be treated as string
* object, string, number and Boolean
*/
class SecureLocalStorage {
class SecureStorage {
private _storage: Storage;
private _localStorageItems: LocalStorageItem = {};

constructor() {
this._localStorageItems = getAllLocalStorageItems();

constructor(storage: Storage) {
this._storage = storage;
this._localStorageItems = getAllLocalStorageItems(this._storage);
}

/**
Expand All @@ -46,7 +48,7 @@ class SecureLocalStorage {
let parsedKey = KEY_PREFIX + key;
if (key != null) this._localStorageItems[parsedKey] = value;
const encrypt = new EncryptionService();
localStorage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue));
this._storage.setItem(parsedKeyLocal, encrypt.encrypt(parsedValue));
}

/**
Expand All @@ -68,18 +70,19 @@ class SecureLocalStorage {
let value = this._localStorageItems[parsedKey];
let parsedKeyLocal = getLocalKey(key, value);
if (this._localStorageItems[parsedKey] !== undefined) delete this._localStorageItems[parsedKey];
localStorage.removeItem(parsedKeyLocal);
this._storage.removeItem(parsedKeyLocal);
}

/**
* Function to clear secure local storage
*/
clear() {
this._localStorageItems = {};
localStorage.clear();
this._storage.clear();
}
}

const secureLocalStorage = new SecureLocalStorage();

const secureLocalStorage = new SecureStorage(localStorage);
export default secureLocalStorage;

export const secureSessionStorage = new SecureStorage(sessionStorage);
4 changes: 2 additions & 2 deletions src/lib/localStorageHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const KEY_PREFIX = getSecurePrefix();
* Function to preload all the local storage data
* @returns
*/
const getAllLocalStorageItems = () => {
const getAllLocalStorageItems = (storage: Storage) => {
const localStorageItems: LocalStorageItem = {};
if (typeof window !== "undefined") {
const encrypt = new EncryptionService();
for (const [key, value] of Object.entries(localStorage)) {
for (const [key, value] of Object.entries(storage)) {
if (key.startsWith(KEY_PREFIX)) {
let keyType = key.replace(KEY_PREFIX, "")[0];
let parsedKey = key.replace(/[.][bjns][.]/, ".");
Expand Down
Loading