Skip to content

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

2.7 KB
Raw Blame
/* eslint-disable @typescript-eslint/no-use-before-define */
import { type GetServerSideProps, type GetServerSidePropsResult } from "next";

import { env } from "@/src/env.mjs";
import { getServerAuthSession } from "@/src/server/auth";
import { isProjectMemberOrAdmin } from "@/src/server/utils/checkProjectMembershipOrAdmin";
import { prisma } from "@langfuse/shared/src/db";

const DemoRedirectPage = () => null;

export default DemoRedirectPage;

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  const demoProject =
    env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION &&
    env.NEXT_PUBLIC_DEMO_ORG_ID &&
    env.NEXT_PUBLIC_DEMO_PROJECT_ID
      ? await prisma.project.findUnique({
          where: {
            orgId: env.NEXT_PUBLIC_DEMO_ORG_ID,
            id: env.NEXT_PUBLIC_DEMO_PROJECT_ID,
            deletedAt: null,
          },
          select: {
            id: true,
          },
        })
      : null;

  if (!demoProject) {
    return redirect("/");
  }

  const { demoTargetPath, demoProjectPath } = getDemoRedirectPaths({
    resolvedUrl: ctx.resolvedUrl,
    projectId: demoProject.id,
  });
  const session = await getServerAuthSession({ req: ctx.req, res: ctx.res });

  if (isProjectMemberOrAdmin(session?.user, demoProject.id)) {
    return redirect(demoProjectPath);
  }

  if (session) {
    return redirect("/");
  }

  const authPath =
    env.AUTH_DISABLE_SIGNUP === "true" ||
    env.NEXT_PUBLIC_SIGN_UP_DISABLED === "true"
      ? "/auth/sign-in"
      : "/auth/sign-up";

  return redirect(
    `${authPath}?targetPath=${encodeURIComponent(demoTargetPath)}`,
  );
};

const redirect = (destination: string): GetServerSidePropsResult<never> => ({
  redirect: { destination, permanent: false },
});

function getDemoRedirectPaths({
  resolvedUrl,
  projectId,
}: {
  resolvedUrl: string;
  projectId: string;
}) {
  const encodedProjectId = encodeURIComponent(projectId);

  try {
    const url = new URL(resolvedUrl, "https://langfuse.invalid");

    if (url.pathname.startsWith("/demo/")) {
      const demoSuffix = url.pathname.slice("/demo".length);
      const pathSuffix = `${url.search}${url.hash}`;

      return {
        demoTargetPath: `${url.pathname}${pathSuffix}`,
        demoProjectPath: `/project/${encodedProjectId}${demoSuffix}${pathSuffix}`,
      };
    }

    if (url.pathname === "/demo") {
      const pathSuffix = `${url.search}${url.hash}`;

      return {
        demoTargetPath: `/demo${pathSuffix}`,
        demoProjectPath: `/project/${encodedProjectId}/traces${pathSuffix}`,
      };
    }
  } catch {
    // Fall through to the stable default below.
  }

  return {
    demoTargetPath: "/demo",
    demoProjectPath: `/project/${encodedProjectId}/traces`,
  };
}