diff --git a/app/dashboard/app/providers/posthog-provider.tsx b/app/dashboard/app/providers/posthog-provider.tsx index cff2eec39..ca9a1c100 100644 --- a/app/dashboard/app/providers/posthog-provider.tsx +++ b/app/dashboard/app/providers/posthog-provider.tsx @@ -11,13 +11,16 @@ import { PostHogProvider as PHProvider } from 'posthog-js/react'; export function PostHogProvider({ children }: { children: React.ReactNode }) { useEffect(() => { const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY; + const isProd = process.env.NODE_ENV === 'production'; + const enableInDev = process.env.NEXT_PUBLIC_ENABLE_POSTHOG_IN_DEV === 'true'; - // Only initialize PostHog if we have a valid key - if (posthogKey) { + // Only initialize PostHog if we have a valid key and it's production + // or explicitly enabled for development. + if (posthogKey && (isProd || enableInDev)) { posthog.init(posthogKey, { api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', - person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well - capture_pageview: false, // Disable automatic pageview capture, as we capture manually + person_profiles: 'identified_only', + capture_pageview: false, }); } }, []); diff --git a/app/dashboard/app/signin/auth-form.tsx b/app/dashboard/app/signin/auth-form.tsx index e28df6793..5da3d2aae 100644 --- a/app/dashboard/app/signin/auth-form.tsx +++ b/app/dashboard/app/signin/auth-form.tsx @@ -40,6 +40,11 @@ export function AuthForm() { } const checkAuth = async () => { + // If there is no session cookie, skip calling the API to avoid 401 spam in dev + if (typeof document !== 'undefined' && !document.cookie.includes('session_id=')) { + setIsCheckingAuth(false); + return; + } try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/opsboard/users/me`, { method: 'GET', diff --git a/app/dashboard/app/signin/components.tsx b/app/dashboard/app/signin/components.tsx index 892e277b0..480a2a2bc 100644 --- a/app/dashboard/app/signin/components.tsx +++ b/app/dashboard/app/signin/components.tsx @@ -15,7 +15,7 @@ import { GithubIcon as Github, Loading03Icon as Loader } from 'hugeicons-react'; import { MagicWand01Icon as WandSparkles } from 'hugeicons-react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; -import posthog from 'posthog-js'; +// posthog is not required directly in this file; remove unused import to prevent bundling warnings import { useState, useEffect } from 'react'; import { toast } from '@/components/ui/use-toast'; diff --git a/app/dashboard/app/simple-login/page.tsx b/app/dashboard/app/simple-login/page.tsx index 60bc3e566..dd2e454d6 100644 --- a/app/dashboard/app/simple-login/page.tsx +++ b/app/dashboard/app/simple-login/page.tsx @@ -52,6 +52,10 @@ const SimpleLoginPageDev = () => { const authBaseUrl = `${apiUrl}/auth`; const checkAuthAndFetchDetails = async () => { + // Avoid unnecessary 401s in local mode: if there is no session cookie, skip the check + if (typeof document !== 'undefined' && !document.cookie.includes('session_id=')) { + return false; + } const lastLoginMethod = localStorage.getItem('loginMethodUsed') as LoginType | null; if (loginMethodUsed !== lastLoginMethod) { setLoginMethodUsed(lastLoginMethod);