Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

1.2 KB
Raw Blame
"use client";

import { useEffect } from "react";
import * as Sentry from "@sentry/nextjs";
import { useAuth } from "@/lib/auth/hooks/useAuth";

/**
 * SentryUserTracker component sets user context in Sentry for error tracking.
 * This component should be placed high in the component tree to ensure user
 * context is available for all error reports.
 *
 * It automatically:
 * - Sets user context when a user logs in
 * - Clears user context when a user logs out
 * - Updates context when user data changes
 */
export function SentryUserTracker() {
  const { user, isUserLoading } = useAuth();

  useEffect(() => {
    if (user) {
      // Wait until user loading is complete before setting user context
      if (isUserLoading) return;

      // The id is enough to find a user's errors; their email stays out of
      // Sentry. The role only tells admins apart.
      Sentry.setUser({
        id: user.id,
        ...(user.role && { role: user.role }),
      });
    } else {
      // Always clear user context when user is null, regardless of loading state
      // This ensures logout properly clears the context immediately
      Sentry.setUser(null);
    }
  }, [user, isUserLoading]);

  // This component doesn't render anything
  return null;
}