Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(auth): Clean up next-auth type definitions (#15032)

Ben Bachemf964c2998803
diff --git a/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts b/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts
index 4b13b8a7e3..59a0a732fd 100644
--- a/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts
+++ b/web/src/components/layouts/app-layout/hooks/useFilteredNavigation.ts
@@ -5,7 +5,7 @@
import { useRouter } from "next/router";
import { useMemo } from "react";
-import type { Session, User } from "next-auth";
+import type { Session } from "next-auth";
import { useEntitlements } from "@/src/features/entitlements/hooks";
import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";
import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";
@@ -21,7 +21,10 @@ import type { NavigationFilterContext } from "../utils/navigationFilters.types";
import { isPathActive } from "../utils/pathClassification";
/** Organization type from user session (can be null when not in project/org context) */
-type Organization = User["organizations"][number] | null | undefined;
+type Organization =
+ | NonNullable<Session["user"]>["organizations"][number]
+ | null
+ | undefined;
/** Grouped navigation structure */
type GroupedNavigation = {
diff --git a/web/src/components/layouts/app-layout/utils/navigationFilters.ts b/web/src/components/layouts/app-layout/utils/navigationFilters.ts
index d700358978..b13f98261f 100644
--- a/web/src/components/layouts/app-layout/utils/navigationFilters.ts
+++ b/web/src/components/layouts/app-layout/utils/navigationFilters.ts
@@ -7,10 +7,13 @@ import type { Route } from "@/src/components/layouts/routes";
import type { NavigationFilterContext } from "./navigationFilters.types";
import { hasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { hasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
-import type { User } from "next-auth";
+import type { Session } from "next-auth";
/** Organization type from user session (can be null when not in project/org context) */
-type Organization = User["organizations"][number] | null | undefined;
+type Organization =
+ | NonNullable<Session["user"]>["organizations"][number]
+ | null
+ | undefined;
/**
* Individual filter functions - each handles one concern
diff --git a/web/src/components/layouts/routes.tsx b/web/src/components/layouts/routes.tsx
index c58a1e3629..f07818ffdd 100644
--- a/web/src/components/layouts/routes.tsx
+++ b/web/src/components/layouts/routes.tsx
@@ -23,7 +23,7 @@ import {
} from "lucide-react";
import { type ReactNode } from "react";
import { type Entitlement } from "@/src/features/entitlements/constants/entitlements";
-import { type User } from "next-auth";
+import { type Session } from "next-auth";
import { type OrganizationScope } from "@/src/features/rbac/constants/organizationAccessRights";
import { SupportButton } from "@/src/components/nav/support-button";
import { InAppAiAgentButton } from "@/src/components/nav/in-app-ai-agent-button";
@@ -62,7 +62,9 @@ export type Route = {
entitlements?: Entitlement[]; // entitlements required, array treated as OR
productModule?: ProductModule; // Product module this route belongs to. Used to show/hide modules via ui customization.
show?: (p: {
- organization: User["organizations"][number] | undefined;
+ organization:
+ | NonNullable<Session["user"]>["organizations"][number]
+ | undefined;
projectId: string | undefined;
isLangfuseCloud: boolean;
v4WriteMode: undefined | "legacy" | "dual" | "events_only"; // undefined until the session has loaded
diff --git a/web/src/features/entitlements/server/hasEntitlement.ts b/web/src/features/entitlements/server/hasEntitlement.ts
index 8d8db66cb5..d36898598c 100644
--- a/web/src/features/entitlements/server/hasEntitlement.ts
+++ b/web/src/features/entitlements/server/hasEntitlement.ts
@@ -3,12 +3,12 @@ import {
type Entitlement,
} from "@/src/features/entitlements/constants/entitlements";
import { TRPCError } from "@trpc/server";
-import { type User } from "next-auth";
+import { type Session } from "next-auth";
import { type Plan } from "@langfuse/shared";
type HasEntitlementParams = {
entitlement: Entitlement;
- sessionUser: User;
+ sessionUser: NonNullable<Session["user"]>;
} & ({ projectId: string } | { orgId: string });
/**
diff --git a/web/src/features/entitlements/server/hasEntitlementLimit.ts b/web/src/features/entitlements/server/hasEntitlementLimit.ts
index 38484157d1..6317fd2067 100644
--- a/web/src/features/entitlements/server/hasEntitlementLimit.ts
+++ b/web/src/features/entitlements/server/hasEntitlementLimit.ts
@@ -4,11 +4,11 @@ import {
} from "@/src/features/entitlements/constants/entitlements";
import { type Plan } from "@langfuse/shared";
import { TRPCError } from "@trpc/server";
-import { type User } from "next-auth";
+import { type Session } from "next-auth";
type HasEntitlementLimitParams = {
entitlementLimit: EntitlementLimit;
- sessionUser: User;
+ sessionUser: NonNullable<Session["user"]>;
} & ({ projectId: string } | { orgId: string });
/**
diff --git a/web/src/features/organizations/components/ProjectOverview.tsx b/web/src/features/organizations/components/ProjectOverview.tsx
index 798d2e615e..86e17af23b 100644
--- a/web/src/features/organizations/components/ProjectOverview.tsx
+++ b/web/src/features/organizations/components/ProjectOverview.tsx
@@ -31,7 +31,7 @@ import {
} from "@/src/features/setup/setupRoutes";
import { isCloudPlan, planLabels } from "@langfuse/shared";
import ContainerPage from "@/src/components/layouts/container-page";
-import { type User } from "next-auth";
+import { type Session } from "next-auth";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { AgentToolsBanner } from "@/src/features/developer-tools/components/AgentToolsBanner";
@@ -39,7 +39,7 @@ const OrganizationProjectTiles = ({
org,
search,
}: {
- org: User["organizations"][number];
+ org: NonNullable<Session["user"]>["organizations"][number];
search?: string;
}) => {
return (
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index 6066ec28f4..31fc440eb2 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -1,7 +1,6 @@
import { type GetServerSidePropsContext } from "next";
import {
getServerSession,
- type User,
type NextAuthOptions,
type Session,
} from "next-auth";
@@ -123,7 +122,7 @@ const staticProviders: Provider[] = [
);
if (!isValidPassword) throw new Error("Invalid credentials");
- const userObj: User = {
+ const userObj = {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
diff --git a/web/src/server/utils/checkProjectMembershipOrAdmin.ts b/web/src/server/utils/checkProjectMembershipOrAdmin.ts
index e23fd106fb..d244017848 100644
--- a/web/src/server/utils/checkProjectMembershipOrAdmin.ts
+++ b/web/src/server/utils/checkProjectMembershipOrAdmin.ts
@@ -1,7 +1,7 @@
-import { type User } from "next-auth";
+import { type Session } from "next-auth";
export const isProjectMemberOrAdmin = (
- user: User | null | undefined,
+ user: Session["user"] | undefined,
projectId: string,
): boolean => {
if (!user) return false;
diff --git a/web/types/next-auth.d.ts b/web/types/next-auth.d.ts
index d841a8e654..24bb2b71ea 100644
--- a/web/types/next-auth.d.ts
+++ b/web/types/next-auth.d.ts
@@ -1,4 +1,4 @@
-import { type DefaultSession, type DefaultUser } from "next-auth";
+import { type DefaultSession } from "next-auth";
import {
type User as PrismaUser,
type Project as PrismaProject,
@@ -17,7 +17,43 @@ import { type Plan } from "@langfuse/shared";
*/
declare module "next-auth" {
interface Session extends DefaultSession {
- user: User | null; // null if user does not exist anymore in the database but has active jwt
+ user:
+ | ({
+ id: PrismaUser["id"];
+ name?: PrismaUser["name"];
+ email?: PrismaUser["email"];
+ emailSupportHash?: string | null;
+ image?: PrismaUser["image"];
+ admin?: PrismaUser["admin"];
+ v4BetaEnabled?: boolean;
+ canToggleV4?: boolean;
+ emailVerified?: string | null; // iso datetime string, need to stringify as JWT & useSession do not support Date objects
+ canCreateOrganizations: boolean; // default true, allowlist can be set via LANGFUSE_ALLOWED_ORGANIZATION_CREATORS
+ organizations: {
+ id: PrismaOrganization["id"];
+ name: PrismaOrganization["name"];
+ role: Role;
+ cloudConfig: CloudConfigSchema | undefined;
+ plan: Plan;
+ metadata: Record<string, unknown>;
+ aiFeaturesEnabled: boolean;
+ aiTelemetryEnabled: boolean;
+ projects: {
+ id: PrismaProject["id"];
+ name: PrismaProject["name"];
+ deletedAt: PrismaProject["deletedAt"];
+ retentionDays: PrismaProject["retentionDays"];
+ hasTraces: PrismaProject["hasTraces"];
+ metadata: Record<string, unknown>;
+ role: Role; // include only projects where user has a role
+ createdAt: string; // iso datetime string — JWT does not support Date objects
+ }[];
+ }[];
+ featureFlags: Flags;
+ hasPassword?: boolean;
+ } & DefaultSession["user"])
+ | null; // null if user does not exist anymore in the database but has active jwt
+
environment: {
// Run-time environment variables that need to be available client-side
enableExperimentalFeatures: boolean;
@@ -32,40 +68,9 @@ declare module "next-auth" {
};
}
- interface User extends DefaultUser {
- id: PrismaUser["id"];
- name?: PrismaUser["name"];
- email?: PrismaUser["email"];
- emailSupportHash?: string | null;
- image?: PrismaUser["image"];
- admin?: PrismaUser["admin"];
- v4BetaEnabled?: boolean;
- canToggleV4?: boolean;
- emailVerified?: string | null; // iso datetime string, need to stringify as JWT & useSession do not support Date objects
- canCreateOrganizations: boolean; // default true, allowlist can be set via LANGFUSE_ALLOWED_ORGANIZATION_CREATORS
- organizations: {
- id: PrismaOrganization["id"];
- name: PrismaOrganization["name"];
- role: Role;
- cloudConfig: CloudConfigSchema | undefined;
- plan: Plan;
- metadata: Record<string, unknown>;
- aiFeaturesEnabled: boolean;
- aiTelemetryEnabled: boolean;
- projects: {
- id: PrismaProject["id"];
- name: PrismaProject["name"];
- deletedAt: PrismaProject["deletedAt"];
- retentionDays: PrismaProject["retentionDays"];
- hasTraces: PrismaProject["hasTraces"];
- metadata: Record<string, unknown>;
- role: Role; // include only projects where user has a role
- createdAt: string; // iso datetime string — JWT does not support Date objects
- }[];
- }[];
- featureFlags: Flags;
- hasPassword?: boolean;
- }
+ // Do not add `interface User extends DefaultUser` here.
+ // OAuth provider `profile` callbacks return `User` before the session callback enriches it.
+ // App-specific fields therefore belong on `Session.user`.
}
declare module "next-auth/jwt" {