🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(preview): sign in automatically as the demo user on previews (#15579)
Trang Nguyenfdb3e6902be1
diff --git a/.agents/skills/langfuse-previews/SKILL.md b/.agents/skills/langfuse-previews/SKILL.mdindex 18203e3793..d505cfeef7 100644--- a/.agents/skills/langfuse-previews/SKILL.md+++ b/.agents/skills/langfuse-previews/SKILL.md@@ -36,10 +36,15 @@ Pushing updates it; closing the PR tears it down.pinned at the top of the PR description (removed again on teardown), and adeployment in the shared GitHub `PR Preview` environment gives the PR a**View deployment** button. No manual step, no label to add.-- **Log in** — use the credentials in the **bot's PR comment** (the source of- truth). The demo project's shared seed identity is `[email protected]` /- `password`, with API keys `pk-lf-1234567890` / `sk-lf-1234567890` — shared and- synthetic, so never treat a preview as private.+- **Log in** — previews sign you in automatically as the shared demo user; just+ open the URL. To use the regular sign-in flow instead (e.g. to test auth+ changes), open `/auth/sign-in?autoSignIn=false` and use the credentials in+ the **bot's PR comment** (the source of truth); signing out via the UI also+ lands on that opted-out form, so you stay signed out until you next open a+ protected page. The demo project's+ shared seed identity is `[email protected]` / `password`, with API keys+ `pk-lf-1234567890` / `sk-lf-1234567890` — shared and synthetic, so never+ treat a preview as private.- **Know where you are** — every preview page shows a top strip linking backto the PR, with the author and when the preview content last changed.- **Update** — push to the PR; it rebuilds and rolls to the new image (~5 min,diff --git a/.github/workflows/preview-build.yml b/.github/workflows/preview-build.ymlindex 9b2b7479ab..18832be205 100644--- a/.github/workflows/preview-build.yml+++ b/.github/workflows/preview-build.yml@@ -170,7 +170,10 @@ jobs:# The cloud region must be baked at build time (NEXT_PUBLIC_* is# inlined into the client bundle) so previews render the cloud-only# AI features; the worker reads it from runtime env instead.- extra_build_args: --build-arg NEXT_PUBLIC_SIGN_UP_DISABLED=true --build-arg NEXT_PUBLIC_LANGFUSE_CLOUD_REGION=DEV+ # Auto sign-in with the shared seeded demo identity — previews are+ # disposable, synthetic-data-only environments with an intentionally+ # public login, so visitors should not have to type it.+ extra_build_args: --build-arg NEXT_PUBLIC_SIGN_UP_DISABLED=true --build-arg NEXT_PUBLIC_LANGFUSE_CLOUD_REGION=DEV --build-arg NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN=true- target: workerimage: ${{ needs.meta.outputs.worker_image }}dockerfile: worker/Dockerfile@@ -305,7 +308,7 @@ jobs:Argo CD is rolling it out — the environment is usually ready within afew minutes of this comment.- **URL:** https://${{ needs.meta.outputs.preview_host }}+ **URL:** https://${{ needs.meta.outputs.preview_host }} (signs you in automatically; opt out with `/auth/sign-in?autoSignIn=false`)**Login:** `${{ needs.meta.outputs.login_email }}` / `${{ needs.meta.outputs.login_password }}`**API keys:** `${{ needs.meta.outputs.public_key }}` / `${{ needs.meta.outputs.secret_key }}`**Commit:** ${{ needs.meta.outputs.commit_sha }}diff --git a/web/Dockerfile b/web/Dockerfileindex ea51374336..4f27ebe725 100644--- a/web/Dockerfile+++ b/web/Dockerfile@@ -84,6 +84,8 @@ ARG NEXT_PUBLIC_PREVIEW_PR_AUTHORENV NEXT_PUBLIC_PREVIEW_PR_AUTHOR=$NEXT_PUBLIC_PREVIEW_PR_AUTHORARG NEXT_PUBLIC_PREVIEW_LAST_UPDATEDENV NEXT_PUBLIC_PREVIEW_LAST_UPDATED=$NEXT_PUBLIC_PREVIEW_LAST_UPDATED+ARG NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN+ENV NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN=$NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_INARG NEXT_PUBLIC_POSTHOG_KEYENV NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEYARG NEXT_PUBLIC_POSTHOG_HOSTdiff --git a/web/src/env.mjs b/web/src/env.mjsindex a32af316f1..38f4ff59af 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -567,6 +567,12 @@ export const env = createEnv({NEXT_PUBLIC_PREVIEW_PR_URL: z.url().optional(),NEXT_PUBLIC_PREVIEW_PR_AUTHOR: z.string().optional(),NEXT_PUBLIC_PREVIEW_LAST_UPDATED: z.iso.datetime().optional(),+ // Signs in visitors as the seeded demo user automatically. Only for+ // disposable environments with synthetic data and a public shared login,+ // e.g. PR preview deployments (.github/workflows/preview-build.yml).+ NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN: z+ .enum(["true", "false"])+ .default("false"),NEXT_PUBLIC_POSTHOG_KEY: z.string().optional(),NEXT_PUBLIC_POSTHOG_HOST: z.string().optional(),NEXT_PUBLIC_PLAIN_APP_ID: z.string().optional(),@@ -606,6 +612,8 @@ export const env = createEnv({NEXT_PUBLIC_PREVIEW_PR_AUTHOR: process.env.NEXT_PUBLIC_PREVIEW_PR_AUTHOR,NEXT_PUBLIC_PREVIEW_LAST_UPDATED:process.env.NEXT_PUBLIC_PREVIEW_LAST_UPDATED,+ NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN:+ process.env.NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN,LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES:process.env.LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES,AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,diff --git a/web/src/features/auth/lib/signOut.ts b/web/src/features/auth/lib/signOut.tsindex 36c61bc358..37ef1a9f9c 100644--- a/web/src/features/auth/lib/signOut.ts+++ b/web/src/features/auth/lib/signOut.ts@@ -17,7 +17,14 @@ export const signOutCleanly = async () => {if (env.NEXT_PUBLIC_POSTHOG_KEY && env.NEXT_PUBLIC_POSTHOG_HOST) {posthog.reset();}+ // On preview deployments the sign-in page signs visitors back in+ // automatically; an explicit sign-out must land on the opted-out form or+ // staying signed out via the UI is impossible.+ const autoSignInOptOut =+ env.NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN === "true"+ ? "?autoSignIn=false"+ : "";await signOut({- callbackUrl: `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/auth/sign-in`,+ callbackUrl: `${env.NEXT_PUBLIC_BASE_PATH ?? ""}/auth/sign-in${autoSignInOptOut}`,});};diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsxindex bc7d02c4c8..68f25e909b 100644--- a/web/src/pages/auth/sign-in.tsx+++ b/web/src/pages/auth/sign-in.tsx@@ -25,10 +25,10 @@ import {SiWordpress,} from "react-icons/si";import { TbBrandAzure, TbBrandOauth } from "react-icons/tb";-import { signIn } from "next-auth/react";+import { signIn, useSession } from "next-auth/react";import Head from "next/head";import Link from "next/link";-import { useState, useEffect } from "react";+import { useState, useEffect, useRef } from "react";import { useForm } from "react-hook-form";import * as z from "zod";import { CloudPrivacyNotice } from "@/src/features/auth/components/AuthCloudPrivacyNotice";@@ -46,6 +46,14 @@ import { AuthProviderButton } from "@/src/features/auth/components/AuthProviderBimport { cn } from "@/src/utils/tailwind";import { useLangfuseCloudRegion } from "@/src/features/organizations/hooks";import { getSafeRedirectPath } from "@/src/utils/redirect";+import { Spinner } from "@/src/components/layouts/spinner";++// The shared, intentionally-public demo identity created by the seed script+// (packages/shared/scripts/seeder/seed-postgres.ts) and posted in every+// preview PR comment (.github/workflows/preview-build.yml). Only used when+// NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN is baked into the build.+const PREVIEW_DEMO_USER_EMAIL = "[email protected]";+const PREVIEW_DEMO_USER_PASSWORD = "password";const credentialAuthForm = z.object({email: z.email(),@@ -646,6 +654,60 @@ export default function SignIn({}}+ // Auto sign-in for disposable preview deployments: the flag is baked only+ // into preview images (.github/workflows/preview-build.yml) whose seeded+ // demo login is shared and public anyway. `?autoSignIn=false` opts out,+ // e.g. to exercise the regular auth flows on a preview. NextAuth error+ // redirects land on this page, so an error in the query keeps the form+ // visible instead of silently signing in over it.+ const autoSignInParam = router.query.autoSignIn;+ const autoSignInOptedOut = Array.isArray(autoSignInParam)+ ? autoSignInParam.includes("false")+ : autoSignInParam === "false";+ const previewAutoSignInEnabled =+ env.NEXT_PUBLIC_PREVIEW_DEMO_AUTO_SIGN_IN === "true" &&+ authProviders.credentials &&+ !autoSignInOptedOut &&+ !nextAuthError;+ const [previewAutoSignInPending, setPreviewAutoSignInPending] = useState(+ previewAutoSignInEnabled,+ );+ const previewAutoSignInAttempted = useRef(false);+ const sessionStatus = useSession().status;+ useEffect(() => {+ if (+ !previewAutoSignInEnabled ||+ previewAutoSignInAttempted.current ||+ sessionStatus === "loading"+ )+ return;+ previewAutoSignInAttempted.current = true;+ if (sessionStatus === "authenticated") {+ // already signed in — useAuthGuard navigates away from this page+ return;+ }+ // re-arm in case the flag flipped enabled after mount (query-only nav)+ setPreviewAutoSignInPending(true);+ signIn("credentials", {+ email: PREVIEW_DEMO_USER_EMAIL,+ password: PREVIEW_DEMO_USER_PASSWORD,+ callbackUrl: targetPath ?? "/",+ redirect: false,+ })+ .then((result) => {+ if (result?.ok) return; // session updates and useAuthGuard navigates+ setPreviewAutoSignInPending(false);+ setCredentialsFormError(+ result?.error ?? "Automatic preview sign-in failed.",+ );+ })+ .catch((error) => {+ captureUnknownError("auth.signIn.previewAutoSignIn", error);+ setPreviewAutoSignInPending(false);+ setCredentialsFormError("Automatic preview sign-in failed.");+ });+ }, [previewAutoSignInEnabled, sessionStatus, targetPath]);+/*** First-step handler ("Continue" button).* 1. Validates email.@@ -719,6 +781,17 @@ export default function SignIn({}}+ if (previewAutoSignInEnabled && previewAutoSignInPending) {+ return (+ <>+ <Head>+ <title>Sign in | Langfuse</title>+ </Head>+ <Spinner message={`Signing in as ${PREVIEW_DEMO_USER_EMAIL}`} />+ </>+ );+ }+return (<><Head>