Also handle case where there are no cookies yet

This commit is contained in:
Marshall Polaris 2022-07-19 01:35:34 -07:00
parent c256e9c0cc
commit 55775d9d37

View File

@ -23,5 +23,11 @@ export const setCookie = (name: string, val: string, opts?: CookieOptions) => {
// Note that this intentionally ignores the case where multiple cookies have // Note that this intentionally ignores the case where multiple cookies have
// the same name but different paths. Hopefully we never need to think about it. // the same name but different paths. Hopefully we never need to think about it.
export const getCookies = (cookies: string) => export const getCookies = (cookies: string) => {
Object.fromEntries(cookies.split(';').map(decodeCookie)) const data = cookies.trim()
if (!data) {
return {}
} else {
return Object.fromEntries(data.split(';').map(decodeCookie))
}
}