Skip to content

Commit 87cbd4f

Browse files
committed
fix: ignore expired cookies
1 parent ea43219 commit 87cbd4f

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

lib/utils/getCookie.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
export function getCookie(name: string): string | null {
2-
const value = `; ${document.cookie}`;
3-
console.log("value", value);
4-
const parts = value.split(`; ${name}=`);
5-
console.log("parts.length", parts);
6-
if (parts.length === 2) return parts.pop()?.split(";").shift() || null;
7-
return null;
2+
const cookies = document.cookie.split(';');
3+
for (let i = 0; i < cookies.length; i++) {
4+
const cookie = cookies[i].trim();
5+
if (cookie.startsWith(name + '=')) {
6+
const cookieValue = cookie.substring(name.length + 1);
7+
const parts = cookieValue.split(';');
8+
let hasExpiration = false;
9+
for (let j = 0; j < parts.length; j++) {
10+
const part = parts[j].trim();
11+
if (part.startsWith('expires=')) {
12+
hasExpiration = true;
13+
const expirationDate = new Date(part.substring(8));
14+
if (expirationDate > new Date()) {
15+
return cookieValue || null; // Cookie is valid
16+
} else {
17+
return null; // Cookie is expired
18+
}
19+
}
20+
}
21+
if (!hasExpiration) {
22+
return cookieValue || null; // Cookie has no expiration, so it's valid
23+
}
24+
}
25+
}
26+
return null; // Cookie not found
827
}

0 commit comments

Comments
 (0)