story-research-zapwall/lib/cookieCleanup.ts

34 lines
919 B
TypeScript

/**
* Cookie cleanup utilities
* Removes all cookies from the domain
*/
/**
* Delete all cookies for the current domain
*/
export function deleteAllCookies(): void {
if (typeof document === 'undefined') {
return
}
const cookies = document.cookie.split(';')
for (let i = 0; i < cookies.length; i += 1) {
const cookie = cookies[i]
if (!cookie) {
continue
}
const eqPos = cookie.indexOf('=')
const name = eqPos > -1 ? cookie.substring(0, eqPos).trim() : cookie.trim()
if (!name) {
continue
}
// Delete cookie by setting it to expire in the past
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=${window.location.hostname}`
document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/;domain=.${window.location.hostname}`
}
}