Skip to content

Commit

Permalink
Self heal if organization is an orphan (#258)
Browse files Browse the repository at this point in the history
* Fix: Self heal if org doesn't have workspace

* Self heal
  • Loading branch information
perkinsjr authored Sep 16, 2023
1 parent aab1458 commit d1a3223
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const CreateWorkspace: React.FC<Props> = ({ 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",
});
},
Expand Down
39 changes: 23 additions & 16 deletions apps/web/middleware.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,32 +40,39 @@ 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)) {
return redirectToSignIn({ returnBackUrl: req.url });
}
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 }));
Expand Down

1 comment on commit d1a3223

@vercel
Copy link

@vercel vercel bot commented on d1a3223 Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

unkey – ./

unkey.vercel.app
unkey-git-main-unkey.vercel.app
unkey-unkey.vercel.app
www.unkey.dev
unkey.dev

Please sign in to comment.