UNPKG

@web3auth/no-modal

Version:
35 lines (32 loc) 1.36 kB
import { WEB3AUTH_STATE_STORAGE_KEY } from './constants.js'; import { deserialize } from './deserialize.js'; const cookieStorage = options => ({ getItem(key) { if (typeof window === "undefined") return null; const value = parseCookie(document.cookie, key); return value !== null && value !== void 0 ? value : null; }, setItem(key, value) { if (typeof window === "undefined") return; let cookieString = `${key}=${value};path=/;samesite=Lax`; if (options !== null && options !== void 0 && options.expiry && typeof options.expiry === "number") cookieString += `; expires=${new Date(Date.now() + options.expiry).toUTCString()}`; if (process.env.NODE_ENV === "production") cookieString += "; secure"; document.cookie = cookieString; }, removeItem(key) { if (typeof window === "undefined") return; document.cookie = `${key}=;max-age=-1;path=/`; } }); function cookieToWeb3AuthState(cookie) { if (!cookie) return undefined; const parsed = parseCookie(cookie, WEB3AUTH_STATE_STORAGE_KEY); if (!parsed) return undefined; return deserialize(parsed); } function parseCookie(cookie, key) { const keyValue = cookie.split("; ").find(x => x.startsWith(`${key}=`)); if (!keyValue) return undefined; return keyValue.substring(key.length + 1); } export { cookieStorage, cookieToWeb3AuthState, parseCookie };