NAM-APJATEL/src/stores/storage.ts

26 lines
696 B
TypeScript

import { StateStorage } from "zustand/middleware";
const KEY = import.meta.env.VITE_APP_KEY;
export const secureStorage: StateStorage = {
getItem: (name: string): string | null => {
const data = localStorage.getItem(name);
if (!data) return null;
try {
const decrypted = CryptoJS.AES.decrypt(data, KEY).toString(
CryptoJS.enc.Utf8
);
return decrypted || null;
} catch {
return null;
}
},
setItem: (name: string, value: string): void => {
const encrypted = CryptoJS.AES.encrypt(value, KEY).toString();
localStorage.setItem(name, encrypted);
},
removeItem: (name: string): void => {
localStorage.removeItem(name);
},
};