membros-react-sdk
Version:
React authentication library for Membros platform with subscription management and plan-based access control
75 lines • 2.39 kB
JavaScript
// Cookie utilities to replace nookies dependency
// Provides the same interface as nookies for seamless replacement
/**
* Set a cookie with the given name, value and options
*/
export const setCookie = (ctx, // For compatibility with nookies API (not used in client-side)
name, value, options = {}) => {
if (typeof window === 'undefined') {
// Server-side handling would go here if needed
return;
}
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
if (options.path) {
cookieString += `; path=${options.path}`;
}
if (options.domain) {
cookieString += `; domain=${options.domain}`;
}
if (options.maxAge) {
cookieString += `; max-age=${options.maxAge}`;
}
if (options.expires) {
cookieString += `; expires=${options.expires.toUTCString()}`;
}
if (options.secure) {
cookieString += `; secure`;
}
if (options.sameSite) {
cookieString += `; samesite=${options.sameSite}`;
}
if (options.httpOnly) {
cookieString += `; httponly`;
}
document.cookie = cookieString;
};
/**
* Parse cookies from document.cookie and return as an object
*/
export const parseCookies = (ctx) => {
if (typeof window === 'undefined') {
// Server-side handling would go here if needed
return {};
}
const cookies = {};
if (document.cookie) {
document.cookie.split(';').forEach(cookie => {
const [name, value] = cookie.trim().split('=');
if (name && value) {
try {
cookies[decodeURIComponent(name)] = decodeURIComponent(value);
}
catch (e) {
// Skip malformed cookies
console.warn('Failed to parse cookie:', name, value);
}
}
});
}
return cookies;
};
/**
* Remove a cookie by setting its expiration to the past
*/
export const destroyCookie = (ctx, // For compatibility with nookies API (not used in client-side)
name, options = {}) => {
if (typeof window === 'undefined') {
// Server-side handling would go here if needed
return;
}
setCookie(ctx, name, '', {
...options,
expires: new Date(0), // Set expiration to Unix epoch (past date)
});
};
//# sourceMappingURL=cookies.js.map