From 74ea9debd5929d6af3e96ad5ac096889af5a3edc Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:19:30 -0600 Subject: [PATCH 1/2] refactor(auth): add validation to prevent create another admin when a admin is present --- apps/dokploy/server/api/routers/auth.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/dokploy/server/api/routers/auth.ts b/apps/dokploy/server/api/routers/auth.ts index 262c9ac2..7b73445e 100644 --- a/apps/dokploy/server/api/routers/auth.ts +++ b/apps/dokploy/server/api/routers/auth.ts @@ -27,12 +27,21 @@ import { protectedProcedure, publicProcedure, } from "../trpc"; +import { db } from "../../db"; export const authRouter = createTRPCRouter({ createAdmin: publicProcedure .input(apiCreateAdmin) .mutation(async ({ ctx, input }) => { try { + const admin = await db.query.admins.findFirst({}); + + if (admin) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Admin already exists", + }); + } const newAdmin = await createAdmin(input); const session = await lucia.createSession(newAdmin.id || "", {}); ctx.res.appendHeader( From c1420bd6d8eaae08e8280c9d443e75e3fcc70a49 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:22:51 -0600 Subject: [PATCH 2/2] chore: apply biome --- apps/dokploy/server/api/routers/auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/dokploy/server/api/routers/auth.ts b/apps/dokploy/server/api/routers/auth.ts index 7b73445e..f3b2362f 100644 --- a/apps/dokploy/server/api/routers/auth.ts +++ b/apps/dokploy/server/api/routers/auth.ts @@ -12,6 +12,7 @@ import { } from "@/server/db/schema"; import { TRPCError } from "@trpc/server"; import * as bcrypt from "bcrypt"; +import { db } from "../../db"; import { createAdmin, createUser, @@ -27,7 +28,6 @@ import { protectedProcedure, publicProcedure, } from "../trpc"; -import { db } from "../../db"; export const authRouter = createTRPCRouter({ createAdmin: publicProcedure