Cookie Manager

Prev Next

Cookie Manager

CookieManager is a static utility class that provides a consistent, SSR-safe abstraction over document.cookie. All cookie reads and writes across the platform go through this single interface.

API

Image

Method Signature Description
get (name: string) → string | null Reads a cookie by name. Returns null if missing or running server-side.
set (name, value, options?) → void Writes a cookie. Accepts path, maxAge, secure, sameSite, expires.
remove (name, path?) → void Deletes a cookie by setting max-age=0. Path defaults to '/'.
clear () → void Removes all cookies except the consent cookie (vlone-cc=).

TTL Constants

The class exports a CookieTTL map with pre-defined max-age values (in seconds):

Key Duration Usage
identity.accessToken 43 200 (12 h) Short-lived access token.
identity.refreshToken 604 800 (7 d) Long-lived refresh token.
common 43 200 (12 h) Default TTL for general-purpose cookies.

SSR Safety

Every method guards against server-side execution with typeof window !== 'undefined'. On the server, get returns null and mutation methods are no-ops. This allows the same code paths to be used in Next.js server components without branching.

Consent Preservation

clear() iterates over all cookies and removes them individually — except any cookie whose name starts with vlone-cc=. This ensures the user's cookie-consent preference survives a session reset.

Usage

import { CookieManager, CookieTTL } from '@/lib/internal/CookieManager';

// Read
const site = CookieManager.get('site');

// Write with TTL
CookieManager.set('accessToken', token, {
  maxAge: CookieTTL['identity.accessToken'],
  secure: true,
  sameSite: 'Lax',
});

// Remove
CookieManager.remove('accessToken');

// Clear all (preserves consent cookie)
CookieManager.clear();