|
1 | 1 | export function getCookie(name: string): string | 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 |
| - } |
| 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 | + } |
24 | 19 | }
|
| 20 | + } |
| 21 | + if (!hasExpiration) { |
| 22 | + return cookieValue || null; // Cookie has no expiration, so it's valid |
| 23 | + } |
25 | 24 | }
|
26 |
| - return null; // Cookie not found |
| 25 | + } |
| 26 | + return null; // Cookie not found |
27 | 27 | }
|
0 commit comments