(cookieStore: Pick<RequestCookies, 'get'>)
| 27 | * undefined if the cookie is not found or is invalid. |
| 28 | */ |
| 29 | const getPostHogCookie = (cookieStore: Pick<RequestCookies, 'get'>): PostHogCookie | undefined => { |
| 30 | const phCookieKey = `ph_${env.POSTHOG_PAPIK}_posthog`; |
| 31 | const cookie = cookieStore.get(phCookieKey); |
| 32 | |
| 33 | if (!cookie) { |
| 34 | return undefined; |
| 35 | } |
| 36 | |
| 37 | const parsedCookie = (() => { |
| 38 | try { |
| 39 | return JSON.parse(cookie.value); |
| 40 | } catch (e) { |
| 41 | Sentry.captureException(e); |
| 42 | return null; |
| 43 | } |
| 44 | })(); |
| 45 | |
| 46 | if (isPostHogCookie(parsedCookie)) { |
| 47 | return parsedCookie; |
| 48 | } |
| 49 | |
| 50 | return undefined; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Attempts to retrieve the distinct id of the current user. |
no test coverage detected