ServiceOptional

WrCookie

Read, write and delete cookies, safely under SSR.

Installation

Import the class. This entry point has no stylesheet.

import { WrCookie } from 'ngwr/cookie';

Live demo

Saved cookies expire in an hour.

Keys visible: —

Expiration

expires takes seconds or a Date; without it the cookie ends with the session.

cookies.set('session', token, { expires: 3600 }); // in an hour
cookies.set('prefs', value, { expires: new Date('2027-01-01') }); // on a date
cookies.set('csrf', token); // when the browser closes

List + clear

cookies.keys(); // every key this document can see
cookies.clear(); // removes them all, on path '/'

API

NameDescriptionTypeDefault
get(key, fallback?)Read a cookie. Returns fallback (default null) when missing.(key, fallback?) => string | null—
has(key)Is the cookie present?(key) => boolean—
set(key, value, options?)Write a cookie. expires takes a Date or seconds. Defaults: path: '/', sameSite: 'Lax'. sameSite: 'None' adds secure.(key, value, options?) => void—
remove(key, options?)Delete a cookie. Pass the path and domain that set() used.(key, options?) => void—
keys()Every cookie key visible to this document.() => readonly string[]—
clear()Remove every cookie. Uses path: '/'.() => void—

See also