22 lines
683 B
JavaScript
22 lines
683 B
JavaScript
// Thin wrapper over document.cookie. Values must already be cookie-safe
|
|
// (see storage.js, which base64url-encodes everything).
|
|
|
|
const ONE_YEAR = 60 * 60 * 24 * 365;
|
|
|
|
export function readCookie(name) {
|
|
const prefix = `${name}=`;
|
|
for (const part of document.cookie.split('; ')) {
|
|
if (part.startsWith(prefix)) return part.slice(prefix.length);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function writeCookie(name, value) {
|
|
const secure = location.protocol === 'https:' ? '; Secure' : '';
|
|
document.cookie = `${name}=${value}; Max-Age=${ONE_YEAR}; Path=/; SameSite=Lax${secure}`;
|
|
}
|
|
|
|
export function removeCookie(name) {
|
|
document.cookie = `${name}=; Max-Age=0; Path=/; SameSite=Lax`;
|
|
}
|