@gistproduct/web
Version:
Build beautiful in-app flows with no code and deliver them instantly to your app.
26 lines (24 loc) • 663 B
JavaScript
export function setKeyWithExpiryToLocalStore(key, value, ttl) {
const item = {
value: value,
expiry: ttl,
};
localStorage.setItem(key, JSON.stringify(item));
}
export function getKeyFromLocalStore(key) {
const itemStr = localStorage.getItem(key);
if (!itemStr) {
return null;
}
const item = JSON.parse(itemStr);
const now = new Date();
const itemExpiry = new Date(item.expiry);
if (now.getTime() > itemExpiry.getTime()) {
localStorage.removeItem(key);
return null;
}
return item.value;
}
export function clearKeyFromLocalStore(key) {
localStorage.removeItem(key);
}