(url: string)
| 33 | } |
| 34 | |
| 35 | export const parseCookieDomain = (url: string) => { |
| 36 | if (process.env.CUSTOM_COOKIE_DOMAIN) { |
| 37 | return { |
| 38 | domain: process.env.CUSTOM_COOKIE_DOMAIN, |
| 39 | secure: true, |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | if (!url) { |
| 44 | return { |
| 45 | domain: undefined, |
| 46 | secure: false, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const domain = new URL(url); |
| 51 | const hostname = domain.hostname; |
| 52 | |
| 53 | // For localhost or IP addresses, don't set domain |
| 54 | if (hostname === 'localhost' || /^\d+\.\d+\.\d+\.\d+$/.test(hostname)) { |
| 55 | return { |
| 56 | domain: undefined, |
| 57 | secure: domain.protocol === 'https:', |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | const parts = hostname.split('.'); |
| 62 | |
| 63 | // Handle multi-part TLDs like co.uk, com.au, etc. |
| 64 | if (parts.length >= 3) { |
| 65 | const potentialTLD = parts.slice(-2).join('.'); |
| 66 | if (isMultiPartTLD(potentialTLD)) { |
| 67 | // For domains like example.co.uk or subdomain.example.co.uk |
| 68 | // Use the last 3 parts: .example.co.uk |
| 69 | return { |
| 70 | domain: `.${parts.slice(-3).join('.')}`, |
| 71 | secure: domain.protocol === 'https:', |
| 72 | }; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // For regular subdomains, use the last 2 parts |
| 77 | if (parts.length > 2) { |
| 78 | return { |
| 79 | domain: `.${parts.slice(-2).join('.')}`, |
| 80 | secure: domain.protocol === 'https:', |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | // For root domains, use the full domain with leading dot |
| 85 | return { |
| 86 | domain: `.${hostname}`, |
| 87 | secure: domain.protocol === 'https:', |
| 88 | }; |
| 89 | }; |
no test coverage detected