From d1a3223eecb6fafa703b42692ff9aa7edf9e859e Mon Sep 17 00:00:00 2001 From: James P Date: Sat, 16 Sep 2023 10:46:11 -0400 Subject: [PATCH] Self heal if organization is an orphan (#258) * Fix: Self heal if org doesn't have workspace * Self heal --- .../(app)/new/create-workspace.tsx | 2 +- apps/web/middleware.ts | 39 +++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/apps/web/app/(authenticated)/(app)/new/create-workspace.tsx b/apps/web/app/(authenticated)/(app)/new/create-workspace.tsx index 790b5a89bd..2f902f269d 100644 --- a/apps/web/app/(authenticated)/(app)/new/create-workspace.tsx +++ b/apps/web/app/(authenticated)/(app)/new/create-workspace.tsx @@ -59,7 +59,7 @@ export const CreateWorkspace: React.FC = ({ workspaces }) => { onError(err) { toast({ title: "Error", - description: `An error occured while creating your workspace: ${err.message}`, + description: `An error occured while creating your workspace, please contact support.`, variant: "alert", }); }, diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index dc5fc222ea..2e0a7ab5bc 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -1,5 +1,5 @@ import { db, eq, schema } from "@/lib/db"; -import { authMiddleware } from "@clerk/nextjs"; +import { authMiddleware, clerkClient } from "@clerk/nextjs"; import { redirectToSignIn } from "@clerk/nextjs"; import { NextFetchEvent, NextRequest, NextResponse } from "next/server"; const DEBUG_ON = process.env.CLERK_DEBUG === "true"; @@ -40,7 +40,7 @@ export default async function (req: NextRequest, evt: NextFetchEvent) { const res = await authMiddleware({ publicRoutes, signInUrl: "/auth/sign-in", - debug: DEBUG_ON, + debug: true, afterAuth: async (auth, req) => { if (!(auth.userId || auth.isPublicRoute)) { @@ -48,24 +48,31 @@ export default async function (req: NextRequest, evt: NextFetchEvent) { } userId = auth.userId ?? undefined; tenantId = auth.orgId ?? auth.userId ?? undefined; - // Stops users from accessing the application if they have not paid yet. - if ( - auth.orgId && - !["/app/stripe", "/app/apis", "/app", "/new"].includes(req.nextUrl.pathname) - ) { + if (auth.orgId) { const workspace = await findWorkspace({ tenantId: auth.orgId }); - if (workspace?.plan === "free") { - return NextResponse.redirect(new URL("/app/stripe", req.url)); - } - return NextResponse.next(); - } - if (auth.userId && !auth.orgId && req.nextUrl.pathname === "/app/apis") { - const workspace = await findWorkspace({ tenantId: auth.userId }); - if (!workspace) { + // okay if we don't find a workspace, we need to delete the orgId and send them to create a new workspace. + // this should never happen, but if it does, we need to handle it. + if (!workspace && req.nextUrl.pathname !== "/new") { + console.error("Workspace not found for orgId", auth.orgId); + await clerkClient.organizations.deleteOrganization(auth.orgId); + console.log("Deleted orgId", auth.orgId, " sending to create new workspace.") return NextResponse.redirect(new URL("/new", req.url)); } + // this stops users if they haven't paid. + if (!["/app/stripe", "/app/apis", "/app", "/new"].includes(req.nextUrl.pathname)) { + if (workspace?.plan === "free") { + return NextResponse.redirect(new URL("/app/stripe", req.url)); + } + return NextResponse.next(); + } + if (auth.userId && !auth.orgId && req.nextUrl.pathname === "/app/apis") { + const workspace = await findWorkspace({ tenantId: auth.userId }); + if (!workspace) { + return NextResponse.redirect(new URL("/new", req.url)); + } + } } - }, + } })(req, evt); evt.waitUntil(collectPageViewAnalytics({ req, userId, tenantId }));