Skip to content

Open-source status page, incident response and uptime monitoring

fix: server action (#698)

Maximilian Kaske4ef733c7cd38
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/actions.ts b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/actions.ts
index dd139dece..e2c02e5de 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/actions.ts
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/actions.ts
@@ -1,12 +1,34 @@
"use server";
+import { currentUser } from "@clerk/nextjs";
import { Unkey } from "@unkey/api";
+import { db, eq } from "@openstatus/db";
+import { user, usersToWorkspaces, workspace } from "@openstatus/db/src/schema";
+
import { env } from "@/env";
const unkey = new Unkey({ token: env.UNKEY_TOKEN, cache: "no-cache" });
+// REMINDER: server actions should have middlewares to do auth checks
+
export async function create(ownerId: number) {
+ const _user = await currentUser();
+
+ if (!_user) return;
+
+ const allowedWorkspaces = await db
+ .select()
+ .from(usersToWorkspaces)
+ .innerJoin(user, eq(user.id, usersToWorkspaces.userId))
+ .innerJoin(workspace, eq(workspace.id, usersToWorkspaces.workspaceId))
+ .where(eq(user.tenantId, _user.id))
+ .all();
+
+ const allowedIds = allowedWorkspaces.map((i) => i.workspace.id);
+
+ if (!allowedIds.includes(ownerId)) return;
+
const key = await unkey.keys.create({
apiId: env.UNKEY_API_ID,
ownerId: String(ownerId),
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/create-form.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/create-form.tsx
index 7cccf831e..fa7bd96fa 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/create-form.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/api-token/_components/create-form.tsx
@@ -35,7 +35,8 @@ export function CreateForm({ ownerId }: { ownerId: number }) {
async function onCreate() {
try {
const res = await create(ownerId);
- if (res.result) {
+ if (!res) toastAction("error");
+ if (res?.result) {
setRawKey(res.result.key);
}
} catch {