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.
Dependencies
Section titled “Dependencies”- Metro UI Core
- DOM Library
Basic Usage
Section titled “Basic Usage”// Set a cookieMetro.cookie.setCookie("user-preference", "dark-mode");
// Get a cookieconst preference = Metro.cookie.getCookie("user-preference");console.log(preference); // "dark-mode"
// Delete a cookieMetro.cookie.delCookie("user-preference");
Get All Cookies
Section titled “Get All Cookies”// Get all cookies as an objectconst allCookies = Metro.cookie.getCookies();console.log(allCookies);
Set Cookie with Options
Section titled “Set Cookie with Options”// Set a cookie with expiration date (in milliseconds)Metro.cookie.setCookie("session-token", "abc123", 3600000); // Expires in 1 hour
// Set a cookie with detailed optionsMetro.cookie.setCookie("user-id", "12345", { path: "/dashboard", domain: "example.com", secure: true, maxAge: 86400, // 24 hours in seconds samesite: "Strict"});
Configure Default Cookie Settings
Section titled “Configure Default Cookie Settings”// Configure default settings for all cookiesMetro.cookieSetup({ path: "/app", secure: true, samesite: "Lax"});
// After this, all cookies will use these defaults unless overridden
Plugin Parameters
Section titled “Plugin Parameters”Parameter | Type | Default | Description |
---|---|---|---|
path | String | ”/“ | The path where the cookie is valid |
expires | String | null | Expiration date in UTC string format |
maxAge | Number | null | Maximum age of the cookie in seconds |
domain | String | null | Domain where the cookie is valid |
secure | Boolean | false | If true, cookie is only sent over HTTPS |
samesite | String | null | CSRF protection setting (“Strict”, “Lax”, or “None”) |
API Methods
Section titled “API Methods”- cookieSetup(options) - Configures default settings for all cookies.
Example of Method Usage
Section titled “Example of Method Usage”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.
Example of Method Usage
Section titled “Example of Method Usage”const allCookies = Metro.cookie.getCookies();console.log(allCookies);
- getCookie(name) - Gets the value of a specific cookie.
Example of Method Usage
Section titled “Example of Method Usage”const preference = Metro.cookie.getCookie("user-preference");
- setCookie(name, value, options) - Sets a cookie with the specified name, value, and options.
Example of Method Usage
Section titled “Example of Method Usage”// With simple expiration time in millisecondsMetro.cookie.setCookie("session-token", "abc123", 3600000);
// With detailed optionsMetro.cookie.setCookie("user-id", "12345", { path: "/dashboard", secure: true, maxAge: 86400});
- delCookie(name) - Deletes a cookie with the specified name.
Example of Method Usage
Section titled “Example of Method Usage”Metro.cookie.delCookie("user-preference");
Examples
Section titled “Examples”User Authentication
Section titled “User Authentication”// Store authentication tokenfunction 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 infunction isLoggedIn() { return Metro.cookie.getCookie("auth-token") !== null;}
// Logout userfunction logout() { Metro.cookie.delCookie("auth-token");}
User Preferences
Section titled “User Preferences”// Save user theme preferencefunction setTheme(theme) { // Store for 1 year const oneYear = 365 * 24 * 60 * 60 * 1000; Metro.cookie.setCookie("user-theme", theme, oneYear);}
// Get user theme preferencefunction getTheme() { return Metro.cookie.getCookie("user-theme") || "light";}
Best Practices
Section titled “Best Practices”- Always use the
secure
flag for cookies containing sensitive information - Consider using the
samesite
attribute to prevent CSRF attacks - Set appropriate expiration times for cookies to enhance security
- Be mindful of cookie size limitations (typically 4KB per cookie)
- For storing complex data, consider serializing to JSON before storing