Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update workspaces.ts

Kiran K97040191ef3c
diff --git a/apps/web/lib/middleware/workspaces.ts b/apps/web/lib/middleware/workspaces.ts
index d699139189..9340a2da74 100644
--- a/apps/web/lib/middleware/workspaces.ts
+++ b/apps/web/lib/middleware/workspaces.ts
@@ -1,4 +1,5 @@
import { UserProps } from "@/lib/types";
+import { prismaEdge } from "@dub/prisma/edge";
import { NextRequest, NextResponse } from "next/server";
import { getDefaultWorkspace } from "./utils/get-default-workspace";
import { getWorkspaceProduct } from "./utils/get-workspace-product";
@@ -19,6 +20,7 @@ export async function WorkspacesMiddleware(req: NextRequest, user: UserProps) {
const defaultWorkspace = await getDefaultWorkspace(user);
+ // If user has a default workspace, redirect them to it
if (defaultWorkspace) {
let redirectPath = path;
if (["/", "/login", "/register", "/workspaces"].includes(path)) {
@@ -38,7 +40,28 @@ export async function WorkspacesMiddleware(req: NextRequest, user: UserProps) {
req.url,
),
);
- } else {
- return NextResponse.redirect(new URL("/onboarding/workspace", req.url));
}
+
+ // Redirect user to the accept invite modal if they have a pending invite
+ const projectInvite = await prismaEdge.projectInvite.findFirst({
+ where: {
+ email: user.email,
+ },
+ select: {
+ project: {
+ select: {
+ slug: true,
+ },
+ },
+ },
+ });
+
+ if (projectInvite) {
+ return NextResponse.redirect(
+ new URL(`/${projectInvite.project.slug}?invite=1`, req.url),
+ );
+ }
+
+ // No default workspace or invite found, redirect to workspace onboarding
+ return NextResponse.redirect(new URL("/onboarding/workspace", req.url));
}