Skip to content

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

feat(auth): Add access to default project upon sign-up (#611)

John HU5bfb02b48ad2
diff --git a/src/env.mjs b/src/env.mjs
index d3fa20d4fc..275d994374 100644
--- a/src/env.mjs
+++ b/src/env.mjs
@@ -33,6 +33,11 @@ export const env = createEnv({
required_error:
"A strong Salt is required to encrypt API keys securely. See: https://langfuse.com/docs/deployment/self-host#deploy-the-container",
}),
+ // Add newly signed up users to default project with role
+ LANGFUSE_DEFAULT_PROJECT_ID: z.string().optional(),
+ LANGFUSE_DEFAULT_PROJECT_ROLE: z
+ .enum(["ADMIN", "MEMBER", "VIEWER"])
+ .optional(),
// AUTH
AUTH_GOOGLE_CLIENT_ID: z.string().optional(),
AUTH_GOOGLE_CLIENT_SECRET: z.string().optional(),
@@ -80,6 +85,9 @@ export const env = createEnv({
LANGFUSE_NEW_USER_SIGNUP_WEBHOOK:
process.env.LANGFUSE_NEW_USER_SIGNUP_WEBHOOK,
SALT: process.env.SALT,
+ // Default project and role
+ LANGFUSE_DEFAULT_PROJECT_ID: process.env.LANGFUSE_DEFAULT_PROJECT_ID,
+ LANGFUSE_DEFAULT_PROJECT_ROLE: process.env.LANGFUSE_DEFAULT_PROJECT_ROLE,
// AUTH
AUTH_GOOGLE_CLIENT_ID: process.env.AUTH_GOOGLE_CLIENT_ID,
AUTH_GOOGLE_CLIENT_SECRET: process.env.AUTH_GOOGLE_CLIENT_SECRET,
diff --git a/src/features/auth/lib/createProjectMembershipsOnSignup.ts b/src/features/auth/lib/createProjectMembershipsOnSignup.ts
new file mode 100644
index 0000000000..7b4205194c
--- /dev/null
+++ b/src/features/auth/lib/createProjectMembershipsOnSignup.ts
@@ -0,0 +1,85 @@
+import { env } from "@/src/env.mjs";
+import { prisma } from "@/src/server/db";
+
+export async function createProjectMembershipsOnSignup(user: {
+ id: string;
+ email: string | null;
+}) {
+ try {
+ // Langfuse Cloud: Demo project access
+ const demoProjectId = env.NEXT_PUBLIC_DEMO_PROJECT_ID
+ ? (
+ await prisma.project.findUnique({
+ where: {
+ id: env.NEXT_PUBLIC_DEMO_PROJECT_ID,
+ },
+ })
+ )?.id
+ : undefined;
+ if (demoProjectId !== undefined) {
+ await prisma.membership.create({
+ data: {
+ projectId: demoProjectId,
+ userId: user.id,
+ role: "VIEWER",
+ },
+ });
+ }
+
+ // set default project access
+ const defaultProjectID = env.LANGFUSE_DEFAULT_PROJECT_ID
+ ? (
+ await prisma.project.findUnique({
+ where: {
+ id: env.LANGFUSE_DEFAULT_PROJECT_ID,
+ },
+ })
+ )?.id
+ : undefined;
+ if (defaultProjectID !== undefined) {
+ await prisma.membership.create({
+ data: {
+ projectId: defaultProjectID,
+ userId: user.id,
+ role: env.LANGFUSE_DEFAULT_PROJECT_ROLE ?? "VIEWER",
+ },
+ });
+ }
+ // Invites do not work for users without emails (some future SSO users)
+ if (user.email) await processMembershipInvitations(user.email, user.id);
+ } catch (e) {
+ console.error("Error assigning project access to new user", e);
+ }
+}
+
+async function processMembershipInvitations(email: string, userId: string) {
+ const invitationsForUser = await prisma.membershipInvitation.findMany({
+ where: {
+ email: email.toLowerCase(),
+ },
+ });
+
+ if (invitationsForUser.length > 0) {
+ const membershipsData = invitationsForUser.map((invitation) => {
+ return {
+ userId: userId,
+ projectId: invitation.projectId,
+ role: invitation.role,
+ };
+ });
+
+ await prisma.$transaction([
+ prisma.membership.createMany({
+ data: membershipsData,
+ }),
+ prisma.membershipInvitation.deleteMany({
+ where: {
+ id: {
+ in: invitationsForUser.map((invitation) => invitation.id),
+ },
+ email: email.toLowerCase(),
+ },
+ }),
+ ]);
+ }
+}
diff --git a/src/features/auth/lib/emailPassword.ts b/src/features/auth/lib/emailPassword.ts
index 3b190ac180..034cf4129e 100644
--- a/src/features/auth/lib/emailPassword.ts
+++ b/src/features/auth/lib/emailPassword.ts
@@ -1,5 +1,4 @@
-import { env } from "@/src/env.mjs";
-import { processMembershipInvitations } from "@/src/features/auth/lib/processMembershipInvitations";
+import { createProjectMembershipsOnSignup } from "@/src/features/auth/lib/createProjectMembershipsOnSignup";
import { prisma } from "@/src/server/db";
import { compare, hash } from "bcryptjs";
@@ -31,37 +30,15 @@ export async function createUserEmailPassword(
throw new Error("User with email already exists. Please sign in.");
}
- // set demoProjectId if env exists and project exists in db
- const demoProjectId = env.NEXT_PUBLIC_DEMO_PROJECT_ID
- ? (
- await prisma.project.findUnique({
- where: {
- id: env.NEXT_PUBLIC_DEMO_PROJECT_ID,
- },
- })
- )?.id
- : undefined;
-
const newUser = await prisma.user.create({
data: {
email: email.toLowerCase(),
password: hashedPassword,
name,
- // if demo project id is set grant user access to it
- ...(demoProjectId
- ? {
- memberships: {
- create: {
- projectId: demoProjectId,
- role: "VIEWER",
- },
- },
- }
- : undefined),
},
});
- await processMembershipInvitations(email, newUser.id);
+ await createProjectMembershipsOnSignup(newUser);
return newUser.id;
}
diff --git a/src/features/auth/lib/processMembershipInvitations.ts b/src/features/auth/lib/processMembershipInvitations.ts
deleted file mode 100644
index 67e7276620..0000000000
--- a/src/features/auth/lib/processMembershipInvitations.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import { prisma } from "@/src/server/db";
-
-export async function processMembershipInvitations(
- email: string,
- userId: string,
-) {
- const invitationsForUser = await prisma.membershipInvitation.findMany({
- where: {
- email: email.toLowerCase(),
- },
- });
-
- if (invitationsForUser.length > 0) {
- const membershipsData = invitationsForUser.map((invitation) => {
- return {
- userId: userId,
- projectId: invitation.projectId,
- role: invitation.role,
- };
- });
-
- await prisma.$transaction([
- prisma.membership.createMany({
- data: membershipsData,
- }),
- prisma.membershipInvitation.deleteMany({
- where: {
- id: {
- in: invitationsForUser.map((invitation) => invitation.id),
- },
- email: email.toLowerCase(),
- },
- }),
- ]);
- }
-}
diff --git a/src/server/auth.ts b/src/server/auth.ts
index e3ba108d66..06cf9c6a14 100644
--- a/src/server/auth.ts
+++ b/src/server/auth.ts
@@ -8,16 +8,16 @@ import {
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "@/src/server/db";
import { verifyPassword } from "@/src/features/auth/lib/emailPassword";
-import { processMembershipInvitations } from "@/src/features/auth/lib/processMembershipInvitations";
import { parseFlags } from "@/src/features/feature-flags/utils";
import { env } from "@/src/env.mjs";
+import { createProjectMembershipsOnSignup } from "@/src/features/auth/lib/createProjectMembershipsOnSignup";
+import { type Adapter } from "next-auth/adapters";
// Providers
import { type Provider } from "next-auth/providers";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import GitHubProvider from "next-auth/providers/github";
-import { type Adapter } from "next-auth/adapters";
// Use secure cookies on https hostnames, exception for Vercel which sets NEXTAUTH_URL without the protocol
const useSecureCookies =
@@ -128,27 +128,7 @@ const extendedPrismaAdapter: Adapter = {
const user = await prismaAdapter.createUser(profile);
- // Demo project access
- const demoProjectId = env.NEXT_PUBLIC_DEMO_PROJECT_ID
- ? (
- await prisma.project.findUnique({
- where: {
- id: env.NEXT_PUBLIC_DEMO_PROJECT_ID,
- },
- })
- )?.id
- : undefined;
- if (demoProjectId !== undefined) {
- await prisma.membership.create({
- data: {
- projectId: demoProjectId,
- userId: user.id,
- role: "VIEWER",
- },
- });
- }
-
- await processMembershipInvitations(user.email, user.id);
+ await createProjectMembershipsOnSignup(user);
return user;
},