Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/dashboard/app/providers/posthog-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +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 environmentType = process.env.NEXT_PUBLIC_ENVIRONMENT_TYPE;

// Only initialize PostHog if we have a valid key
if (posthogKey) {
// Only initialize PostHog when a key is present AND not in explicit local/dev mode
// This prevents noisy 401/404 console errors in local development when PostHog
// assets or keys are not configured.
const shouldInitPosthog = Boolean(
posthogKey && environmentType !== 'development' && environmentType !== 'local',
);

if (shouldInitPosthog) {
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
Expand Down
5 changes: 5 additions & 0 deletions app/dashboard/app/signin/auth-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export function AuthForm() {

const checkAuth = async () => {
try {
// Skip probe when no session cookie to avoid expected 401 noise in local/dev
if (typeof document !== 'undefined' && !document.cookie?.includes('session_id=')) {
setIsCheckingAuth(false);
return;
}
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/opsboard/users/me`, {
method: 'GET',
credentials: 'include',
Expand Down
8 changes: 6 additions & 2 deletions app/dashboard/components/posthog-user-identifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export function PostHogUserIdentifier() {
const posthog = usePostHog();

useEffect(() => {
if (posthog && user?.id) {
// Only interact with PostHog if it's actually loaded
// This prevents errors when analytics are disabled in local development
if (!posthog || !(posthog as any).__loaded) return;

if (user?.id) {
posthog.identify(user.id, {
email: user.email || undefined,
name: user.full_name || undefined,
});
} else if (posthog && !user) {
} else if (!user) {
posthog.reset();
}
}, [posthog, user]);
Expand Down
13 changes: 11 additions & 2 deletions app/dashboard/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,14 @@ export async function fetchAuthenticatedApi<T = any>(
errorBody = 'Failed to read error response body';
}
}
console.error(`[API Client] API Error ${response.status} for ${endpoint}:`, errorBody);
// Avoid noisy console errors for expected 401s in local development or during
// unauthenticated flows (e.g., landing on signin). Still construct ApiError for handling.
const isAuthError = response.status === 401 || response.status === 403;
if (!isAuthError) {
console.error(`[API Client] API Error ${response.status} for ${endpoint}:`, errorBody);
} else if (process.env.NODE_ENV !== 'production') {
console.warn(`[API Client] Auth ${response.status} for ${endpoint}`);
}
throw new ApiError(
`API request failed with status ${response.status}`,
response.status,
Expand All @@ -149,7 +156,9 @@ export async function fetchAuthenticatedApi<T = any>(
if (error.status === 401) {
// Ensure this runs only on the client side
if (typeof window !== 'undefined') {
console.warn('[API Client] Received 401 Unauthorized for ${endpoint}.');
if (process.env.NODE_ENV !== 'production') {
console.warn(`[API Client] Received 401 Unauthorized for ${endpoint}.`);
}
// Prevent infinite loops if signin page itself triggers a 401 somehow
if (window.location.pathname !== '/signin') {
window.location.href = '/signin';
Expand Down
9 changes: 9 additions & 0 deletions app/dashboard/lib/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { fetchAuthenticatedApi, ApiError } from '../api-client'; // Import the n
* Fetches the data for the currently authenticated user using the backend API.
*/
export const fetchUserAPI = async (): Promise<IUser | null> => {
// In local/dev without an authenticated session, avoid hitting the backend
// to reduce console noise and unnecessary network requests.
if (typeof document !== 'undefined') {
const hasSessionCookie = document.cookie?.includes('session_id=');
if (!hasSessionCookie) {
return null;
}
}

const endpoint = '/opsboard/users/me'; // Correct backend endpoint

try {
Expand Down