Skip to content

Cookie

The Cookie component provides a simple and efficient way to manage browser cookies. It offers methods for setting, getting, and deleting cookies with various configuration options.

  • Metro UI Core
  • DOM Library
// Set a cookie
Metro.cookie.setCookie("user-preference", "dark-mode");
// Get a cookie
const preference = Metro.cookie.getCookie("user-preference");
console.log(preference); // "dark-mode"
// Delete a cookie
Metro.cookie.delCookie("user-preference");
// Get all cookies as an object
const allCookies = Metro.cookie.getCookies();
console.log(allCookies);
// Set a cookie with expiration date (in milliseconds)
Metro.cookie.setCookie("session-token", "abc123", 3600000); // Expires in 1 hour
// Set a cookie with detailed options
Metro.cookie.setCookie("user-id", "12345", {
path: "/dashboard",
domain: "example.com",
secure: true,
maxAge: 86400, // 24 hours in seconds
samesite: "Strict"
});
// Configure default settings for all cookies
Metro.cookieSetup({
path: "/app",
secure: true,
samesite: "Lax"
});
// After this, all cookies will use these defaults unless overridden
ParameterTypeDefaultDescription
pathString”/“The path where the cookie is valid
expiresStringnullExpiration date in UTC string format
maxAgeNumbernullMaximum age of the cookie in seconds
domainStringnullDomain where the cookie is valid
secureBooleanfalseIf true, cookie is only sent over HTTPS
samesiteStringnullCSRF protection setting (“Strict”, “Lax”, or “None”)
  • cookieSetup(options) - Configures default settings for all cookies.
Metro.cookieSetup({
path: "/app",
secure: true,
samesite: "Lax"
});
  • getCookies() - Returns all cookies as an object with cookie names as keys and their values as values.
const allCookies = Metro.cookie.getCookies();
console.log(allCookies);
  • getCookie(name) - Gets the value of a specific cookie.
const preference = Metro.cookie.getCookie("user-preference");
  • setCookie(name, value, options) - Sets a cookie with the specified name, value, and options.
// With simple expiration time in milliseconds
Metro.cookie.setCookie("session-token", "abc123", 3600000);
// With detailed options
Metro.cookie.setCookie("user-id", "12345", {
path: "/dashboard",
secure: true,
maxAge: 86400
});
  • delCookie(name) - Deletes a cookie with the specified name.
Metro.cookie.delCookie("user-preference");
// Store authentication token
function login(token) {
// Set cookie to expire in 7 days
const sevenDays = 7 * 24 * 60 * 60 * 1000;
Metro.cookie.setCookie("auth-token", token, {
maxAge: sevenDays / 1000, // Convert to seconds
secure: true,
samesite: "Strict"
});
}
// Check if user is logged in
function isLoggedIn() {
return Metro.cookie.getCookie("auth-token") !== null;
}
// Logout user
function logout() {
Metro.cookie.delCookie("auth-token");
}
// Save user theme preference
function setTheme(theme) {
// Store for 1 year
const oneYear = 365 * 24 * 60 * 60 * 1000;
Metro.cookie.setCookie("user-theme", theme, oneYear);
}
// Get user theme preference
function getTheme() {
return Metro.cookie.getCookie("user-theme") || "light";
}
  1. Always use the secure flag for cookies containing sensitive information
  2. Consider using the samesite attribute to prevent CSRF attacks
  3. Set appropriate expiration times for cookies to enhance security
  4. Be mindful of cookie size limitations (typically 4KB per cookie)
  5. For storing complex data, consider serializing to JSON before storing