The name of the cookie
The value of the cookie (string or object)
Optional configuration for the cookie
// Simple cookie
setCookie('user', 'john');
// Cookie with object value
setCookie('userData', { id: 123, name: 'John' });
// Cookie with expiration
const expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 7); // 7 days from now
setCookie('token', 'abc123', { expiresDate: expiryDate, secure: true });
// Cookie with all options
const sessionExpiry = new Date();
sessionExpiry.setDate(sessionExpiry.getDate() + 1); // 1 day from now
setCookie('session', 'xyz', {
expiresDate: sessionExpiry,
path: '/',
domain: 'example.com',
secure: true,
sameSite: 'Strict'
});
Sets a cookie with the specified name, value, and options. If value is an object, it will be automatically JSON-encoded.