Usage
CookieService has six methods. All of them are synchronous and work with plain strings.
| Method | Returns | Does |
|---|---|---|
check(name) |
boolean |
Whether a cookie with that name is visible to the page. |
get(name) |
string |
The decoded value, or '' if there is no such cookie. |
getAll() |
Record<string, string> |
Every visible cookie, by name. |
set(name, value, options?) |
void |
Writes a cookie. |
delete(name, options?) |
void |
Expires a cookie. |
deleteAll(options?) |
void |
Expires every visible cookie. |
Write a cookie
Section titled “Write a cookie”cookieService.set('token', 'abc123');cookieService.set('token', 'abc123', { expires: 7, path: '/', secure: true, sameSite: 'Strict' });The options are:
| Option | Type | Default |
|---|---|---|
expires |
number (days) or Date |
Session cookie |
path |
string |
Directory of the page’s URL |
domain |
string |
Current host only |
secure |
boolean |
false |
sameSite |
'Lax' | 'Strict' | 'None' |
'Lax' |
partitioned |
boolean |
false |
A positional form, set(name, value, expires?, path?, domain?, secure?, sameSite?, partitioned?), is still supported, but the options object is easier to read.
Read a cookie
Section titled “Read a cookie”if (cookieService.check('token')) { const token: string = cookieService.get('token');}
const all: Record<string, string> = cookieService.getAll();get() returns an empty string, not undefined, when the cookie doesn’t exist. Use check() to tell an empty cookie from a missing one.
Delete a cookie
Section titled “Delete a cookie”cookieService.delete('token', { path: '/' });cookieService.deleteAll({ path: '/' });A cookie is identified by its name and its path and domain, so pass the same path and domain it was set with. To delete a Partitioned cookie, also pass partitioned: true.
What the service guards against
Section titled “What the service guards against”SameSite=NoneandPartitionedneedSecure. Browsers drop such cookies otherwise, soCookieServiceturnssecureon and logs a warning.- Header injection. If
pathordomaincontains;or a control character, the cookie is not written and a warning is logged. - Invalid values. An invalid
expiresdate or unknownsameSitevalue is ignored with a warning. - Other domains. Browsers never let a page set or delete cookies for another site, and no library can change that.