@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
39 lines (35 loc) • 1.44 kB
JavaScript
;
var constants = require('./constants.js');
var deserialize = require('./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, constants.WEB3AUTH_STATE_STORAGE_KEY);
if (!parsed) return undefined;
return deserialize.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);
}
exports.cookieStorage = cookieStorage;
exports.cookieToWeb3AuthState = cookieToWeb3AuthState;
exports.parseCookie = parseCookie;