From 9856502ece59841a1861213746b9df137fe055a1 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 16 Feb 2025 13:55:27 -0600 Subject: [PATCH] refactor: remove old references --- apps/dokploy/__test__/drop/drop.test.test.ts | 2 +- .../server/update-server-config.test.ts | 8 +- apps/dokploy/pages/api/stripe/webhook.ts | 2 +- .../pages/dashboard/project/[projectId].tsx | 11 +- .../services/application/[applicationId].tsx | 16 +- .../services/compose/[composeId].tsx | 15 +- .../services/mariadb/[mariadbId].tsx | 16 +- .../[projectId]/services/mongo/[mongoId].tsx | 15 +- .../[projectId]/services/mysql/[mysqlId].tsx | 15 +- .../services/postgres/[postgresId].tsx | 16 +- .../[projectId]/services/redis/[redisId].tsx | 15 +- apps/dokploy/server/api/routers/admin.ts | 25 +- apps/dokploy/server/api/routers/auth.ts | 241 ++++++++---------- packages/server/src/db/schema/admin.ts | 210 --------------- packages/server/src/db/schema/auth.ts | 130 ---------- packages/server/src/db/schema/certificate.ts | 3 - packages/server/src/db/schema/deployment.ts | 2 +- packages/server/src/db/schema/destination.ts | 8 - packages/server/src/db/schema/git-provider.ts | 3 - packages/server/src/db/schema/index.ts | 2 - packages/server/src/db/schema/notification.ts | 2 - packages/server/src/db/schema/project.ts | 4 - packages/server/src/db/schema/registry.ts | 7 - packages/server/src/db/schema/server.ts | 8 - packages/server/src/db/schema/session.ts | 13 - packages/server/src/db/schema/source.ts | 27 -- packages/server/src/db/schema/ssh-key.ts | 3 - packages/server/src/db/schema/user.ts | 142 +++++++---- packages/server/src/services/admin.ts | 2 - packages/server/src/services/auth.ts | 106 +------- 30 files changed, 250 insertions(+), 819 deletions(-) delete mode 100644 packages/server/src/db/schema/admin.ts delete mode 100644 packages/server/src/db/schema/auth.ts delete mode 100644 packages/server/src/db/schema/source.ts diff --git a/apps/dokploy/__test__/drop/drop.test.test.ts b/apps/dokploy/__test__/drop/drop.test.test.ts index c4b2ba8d..4e6f20d3 100644 --- a/apps/dokploy/__test__/drop/drop.test.test.ts +++ b/apps/dokploy/__test__/drop/drop.test.test.ts @@ -45,7 +45,7 @@ const baseApp: ApplicationNested = { previewWildcard: "", project: { env: "", - adminId: "", + organizationId: "", name: "", description: "", createdAt: "", diff --git a/apps/dokploy/__test__/traefik/server/update-server-config.test.ts b/apps/dokploy/__test__/traefik/server/update-server-config.test.ts index e38c19e4..f7bf3595 100644 --- a/apps/dokploy/__test__/traefik/server/update-server-config.test.ts +++ b/apps/dokploy/__test__/traefik/server/update-server-config.test.ts @@ -5,7 +5,7 @@ vi.mock("node:fs", () => ({ default: fs, })); -import type { Admin, FileConfig } from "@dokploy/server"; +import type { Admin, FileConfig, User } from "@dokploy/server"; import { createDefaultServerTraefikConfig, loadOrCreateConfig, @@ -13,7 +13,7 @@ import { } from "@dokploy/server"; import { beforeEach, expect, test, vi } from "vitest"; -const baseAdmin: Admin = { +const baseAdmin: Partial = { enablePaidFeatures: false, metricsConfig: { containers: { @@ -40,9 +40,7 @@ const baseAdmin: Admin = { cleanupCacheApplications: false, cleanupCacheOnCompose: false, cleanupCacheOnPreviews: false, - createdAt: "", - authId: "", - adminId: "string", + createdAt: new Date(), serverIp: null, certificateType: "none", host: null, diff --git a/apps/dokploy/pages/api/stripe/webhook.ts b/apps/dokploy/pages/api/stripe/webhook.ts index e8416c5d..d9cbedc8 100644 --- a/apps/dokploy/pages/api/stripe/webhook.ts +++ b/apps/dokploy/pages/api/stripe/webhook.ts @@ -1,6 +1,6 @@ import { buffer } from "node:stream/consumers"; import { db } from "@/server/db"; -import { admins, server, users_temp } from "@/server/db/schema"; +import { server, users_temp } from "@/server/db/schema"; import { findAdminById, findUserById } from "@dokploy/server"; import { asc, eq } from "drizzle-orm"; import type { NextApiRequest, NextApiResponse } from "next"; diff --git a/apps/dokploy/pages/dashboard/project/[projectId].tsx b/apps/dokploy/pages/dashboard/project/[projectId].tsx index abedd10f..db70a59a 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId].tsx @@ -201,14 +201,7 @@ const Project = ( const [isBulkActionLoading, setIsBulkActionLoading] = useState(false); const { projectId } = props; const { data: auth } = api.auth.get.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); + const { data, isLoading, refetch } = api.project.one.useQuery({ projectId }); const router = useRouter(); @@ -335,7 +328,7 @@ const Project = ( {data?.description} - {(auth?.role === "owner" || user?.canCreateServices) && ( + {(auth?.role === "owner" || auth?.user?.canCreateServices) && (
diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/application/[applicationId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/application/[applicationId].tsx index a394f320..5bdb2ee6 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/application/[applicationId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/application/[applicationId].tsx @@ -40,7 +40,6 @@ import { cn } from "@/lib/utils"; import { appRouter } from "@/server/api/root"; import { api } from "@/utils/api"; import { validateRequest } from "@dokploy/server/lib/auth"; -// import { validateRequest } from "@dokploy/server"; import { createServerSideHelpers } from "@trpc/react-query/server"; import copy from "copy-to-clipboard"; import { GlobeIcon, HelpCircle, ServerOff, Trash2 } from "lucide-react"; @@ -89,14 +88,6 @@ const Service = ( const { data: isCloud } = api.settings.isCloud.useQuery(); const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); return (
@@ -187,7 +178,8 @@ const Service = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -387,8 +379,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/compose/[composeId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/compose/[composeId].tsx index 8bd3e863..f2c4062a 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/compose/[composeId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/compose/[composeId].tsx @@ -82,14 +82,6 @@ const Service = ( const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); const { data: isCloud } = api.settings.isCloud.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); return (
@@ -181,7 +173,8 @@ const Service = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -382,8 +375,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/mariadb/[mariadbId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/mariadb/[mariadbId].tsx index 9adbad15..b9ad1e22 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/mariadb/[mariadbId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/mariadb/[mariadbId].tsx @@ -63,14 +63,7 @@ const Mariadb = ( const { data } = api.mariadb.one.useQuery({ mariadbId }); const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); + const { data: isCloud } = api.settings.isCloud.useQuery(); return ( @@ -154,7 +147,8 @@ const Mariadb = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -332,8 +326,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/mongo/[mongoId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/mongo/[mongoId].tsx index a5ba6b5d..218e3da9 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/mongo/[mongoId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/mongo/[mongoId].tsx @@ -63,14 +63,6 @@ const Mongo = ( const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); const { data: isCloud } = api.settings.isCloud.useQuery(); @@ -156,7 +148,8 @@ const Mongo = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -334,8 +327,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/mysql/[mysqlId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/mysql/[mysqlId].tsx index 3e17c4fa..87011428 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/mysql/[mysqlId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/mysql/[mysqlId].tsx @@ -62,14 +62,6 @@ const MySql = ( const { data } = api.mysql.one.useQuery({ mysqlId }); const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); const { data: isCloud } = api.settings.isCloud.useQuery(); @@ -156,7 +148,8 @@ const MySql = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -339,8 +332,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/postgres/[postgresId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/postgres/[postgresId].tsx index 0b1d2aee..b78fd569 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/postgres/[postgresId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/postgres/[postgresId].tsx @@ -61,14 +61,7 @@ const Postgresql = ( const [tab, setSab] = useState(activeTab); const { data } = api.postgres.one.useQuery({ postgresId }); const { data: auth } = api.auth.get.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); + const { data: monitoring } = api.admin.getMetricsToken.useQuery(); const { data: isCloud } = api.settings.isCloud.useQuery(); @@ -154,7 +147,8 @@ const Postgresql = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -335,8 +329,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/pages/dashboard/project/[projectId]/services/redis/[redisId].tsx b/apps/dokploy/pages/dashboard/project/[projectId]/services/redis/[redisId].tsx index 853e6688..9ed8c47a 100644 --- a/apps/dokploy/pages/dashboard/project/[projectId]/services/redis/[redisId].tsx +++ b/apps/dokploy/pages/dashboard/project/[projectId]/services/redis/[redisId].tsx @@ -62,14 +62,6 @@ const Redis = ( const { data: auth } = api.auth.get.useQuery(); const { data: monitoring } = api.admin.getMetricsToken.useQuery(); - const { data: user } = api.user.byAuthId.useQuery( - { - authId: auth?.id || "", - }, - { - enabled: !!auth?.id && auth?.role === "member", - }, - ); const { data: isCloud } = api.settings.isCloud.useQuery(); @@ -155,7 +147,8 @@ const Redis = (
- {(auth?.role === "owner" || user?.canDeleteServices) && ( + {(auth?.role === "owner" || + auth?.user?.canDeleteServices) && ( )}
@@ -327,8 +320,8 @@ export async function getServerSideProps( req: req as any, res: res as any, db: null as any, - session: session, - user: user, + session: session as any, + user: user as any, }, transformer: superjson, }); diff --git a/apps/dokploy/server/api/routers/admin.ts b/apps/dokploy/server/api/routers/admin.ts index 55df35af..695cdb33 100644 --- a/apps/dokploy/server/api/routers/admin.ts +++ b/apps/dokploy/server/api/routers/admin.ts @@ -4,7 +4,6 @@ import { apiCreateUserInvitation, apiFindOneToken, apiRemoveUser, - apiUpdateAdmin, apiUpdateWebServerMonitoring, } from "@/server/db/schema"; import { @@ -36,19 +35,17 @@ export const adminRouter = createTRPCRouter({ ...rest, }; }), - update: adminProcedure - .input(apiUpdateAdmin) - .mutation(async ({ input, ctx }) => { - if (ctx.user.rol === "member") { - throw new TRPCError({ - code: "UNAUTHORIZED", - message: "You are not allowed to update this admin", - }); - } - const { id } = await findUserById(ctx.user.id); - // @ts-ignore - return updateAdmin(id, input); - }), + update: adminProcedure.mutation(async ({ input, ctx }) => { + if (ctx.user.rol === "member") { + throw new TRPCError({ + code: "UNAUTHORIZED", + message: "You are not allowed to update this admin", + }); + } + const { id } = await findUserById(ctx.user.id); + // @ts-ignore + return updateAdmin(id, input); + }), createUserInvitation: adminProcedure .input(apiCreateUserInvitation) .mutation(async ({ input, ctx }) => { diff --git a/apps/dokploy/server/api/routers/auth.ts b/apps/dokploy/server/api/routers/auth.ts index 9fc66cf2..ad2fab07 100644 --- a/apps/dokploy/server/api/routers/auth.ts +++ b/apps/dokploy/server/api/routers/auth.ts @@ -1,30 +1,23 @@ import { - apiCreateAdmin, - apiCreateUser, - apiFindOneAuth, - apiLogin, - apiUpdateAuth, - apiVerify2FA, - apiVerifyLogin2FA, - auth, + // apiCreateAdmin, + // apiCreateUser, + // apiFindOneAuth, + // apiLogin, + // apiUpdateAuth, + // apiVerify2FA, + // apiVerifyLogin2FA, + // auth, member, } from "@/server/db/schema"; import { WEBSITE_URL } from "@/server/utils/stripe"; import { - type Auth, IS_CLOUD, - createAdmin, - createUser, - findAuthByEmail, findAuthById, findUserById, generate2FASecret, getUserByToken, - removeAdminByAuthId, sendDiscordNotification, sendEmailNotification, - updateAuthById, - updateUser, validateRequest, verify2FA, } from "@dokploy/server"; @@ -43,81 +36,77 @@ import { } from "../trpc"; export const authRouter = createTRPCRouter({ - createAdmin: publicProcedure - .input(apiCreateAdmin) - .mutation(async ({ ctx, input }) => { - try { - if (!IS_CLOUD) { - const admin = await db.query.admins.findFirst({}); - if (admin) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Admin already exists", - }); - } + createAdmin: publicProcedure.mutation(async ({ ctx, input }) => { + try { + if (!IS_CLOUD) { + 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 newAdmin = await createAdmin(input); - if (IS_CLOUD) { - await sendDiscordNotificationWelcome(newAdmin); - await sendVerificationEmail(newAdmin.id); - return { - status: "success", - type: "cloud", - }; - } - // const session = await lucia.createSession(newAdmin.id || "", {}); - // ctx.res.appendHeader( - // "Set-Cookie", - // lucia.createSessionCookie(session.id).serialize(), - // ); + if (IS_CLOUD) { + await sendDiscordNotificationWelcome(newAdmin); + await sendVerificationEmail(newAdmin.id); return { status: "success", - type: "selfhosted", + type: "cloud", }; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - // @ts-ignore - message: `Error: ${error?.code === "23505" ? "Email already exists" : "Error creating admin"}`, - cause: error, - }); } - }), - createUser: publicProcedure - .input(apiCreateUser) - .mutation(async ({ ctx, input }) => { - try { - const token = await getUserByToken(input.token); - // if (token.isExpired) { - // throw new TRPCError({ - // code: "BAD_REQUEST", - // message: "Invalid token", - // }); - // } + // const session = await lucia.createSession(newAdmin.id || "", {}); + // ctx.res.appendHeader( + // "Set-Cookie", + // lucia.createSessionCookie(session.id).serialize(), + // ); + return { + status: "success", + type: "selfhosted", + }; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + // @ts-ignore + message: `Error: ${error?.code === "23505" ? "Email already exists" : "Error creating admin"}`, + cause: error, + }); + } + }), + createUser: publicProcedure.mutation(async ({ ctx, input }) => { + try { + const token = await getUserByToken(input.token); + // if (token.isExpired) { + // throw new TRPCError({ + // code: "BAD_REQUEST", + // message: "Invalid token", + // }); + // } - // const newUser = await createUser(input); + // const newUser = await createUser(input); - // if (IS_CLOUD) { - // await sendVerificationEmail(token.authId); - // return true; - // } - // const session = await lucia.createSession(newUser?.authId || "", {}); - // ctx.res.appendHeader( - // "Set-Cookie", - // lucia.createSessionCookie(session.id).serialize(), - // ); - return true; - } catch (error) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the user", - cause: error, - }); - } - }), + // if (IS_CLOUD) { + // await sendVerificationEmail(token.authId); + // return true; + // } + // const session = await lucia.createSession(newUser?.authId || "", {}); + // ctx.res.appendHeader( + // "Set-Cookie", + // lucia.createSessionCookie(session.id).serialize(), + // ); + return true; + } catch (error) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Error creating the user", + cause: error, + }); + } + }), - login: publicProcedure.input(apiLogin).mutation(async ({ ctx, input }) => { + login: publicProcedure.mutation(async ({ ctx, input }) => { try { const auth = await findAuthByEmail(input.email); @@ -192,33 +181,31 @@ export const authRouter = createTRPCRouter({ return true; }), - update: protectedProcedure - .input(apiUpdateAuth) - .mutation(async ({ ctx, input }) => { - const currentAuth = await findAuthByEmail(ctx.user.email); + update: protectedProcedure.mutation(async ({ ctx, input }) => { + const currentAuth = await findAuthByEmail(ctx.user.email); - if (input.currentPassword || input.password) { - const correctPassword = bcrypt.compareSync( - input.currentPassword || "", - currentAuth?.password || "", - ); - if (!correctPassword) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Current password is incorrect", - }); - } + if (input.currentPassword || input.password) { + const correctPassword = bcrypt.compareSync( + input.currentPassword || "", + currentAuth?.password || "", + ); + if (!correctPassword) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Current password is incorrect", + }); } - // const auth = await updateAuthById(ctx.user.authId, { - // ...(input.email && { email: input.email.toLowerCase() }), - // ...(input.password && { - // password: bcrypt.hashSync(input.password, 10), - // }), - // ...(input.image && { image: input.image }), - // }); + } + // const auth = await updateAuthById(ctx.user.authId, { + // ...(input.email && { email: input.email.toLowerCase() }), + // ...(input.password && { + // password: bcrypt.hashSync(input.password, 10), + // }), + // ...(input.image && { image: input.image }), + // }); - return auth; - }), + return auth; + }), removeSelfAccount: protectedProcedure .input( z.object({ @@ -279,7 +266,7 @@ export const authRouter = createTRPCRouter({ verifyToken: protectedProcedure.mutation(async () => { return true; }), - one: adminProcedure.input(apiFindOneAuth).query(async ({ input }) => { + one: adminProcedure.query(async ({ input }) => { const auth = await findAuthById(input.id); return auth; }), @@ -287,34 +274,30 @@ export const authRouter = createTRPCRouter({ generate2FASecret: protectedProcedure.query(async ({ ctx }) => { return await generate2FASecret(ctx.user.id); }), - verify2FASetup: protectedProcedure - .input(apiVerify2FA) - .mutation(async ({ ctx, input }) => { - // const auth = await findAuthById(ctx.user.authId); - // await verify2FA(auth, input.secret, input.pin); - // await updateAuthById(auth.id, { - // is2FAEnabled: true, - // secret: input.secret, - // }); - // return auth; - }), + verify2FASetup: protectedProcedure.mutation(async ({ ctx, input }) => { + // const auth = await findAuthById(ctx.user.authId); + // await verify2FA(auth, input.secret, input.pin); + // await updateAuthById(auth.id, { + // is2FAEnabled: true, + // secret: input.secret, + // }); + // return auth; + }), - verifyLogin2FA: publicProcedure - .input(apiVerifyLogin2FA) - .mutation(async ({ ctx, input }) => { - // const auth = await findAuthById(input.id); + verifyLogin2FA: publicProcedure.mutation(async ({ ctx, input }) => { + // const auth = await findAuthById(input.id); - // await verify2FA(auth, auth.secret || "", input.pin); + // await verify2FA(auth, auth.secret || "", input.pin); - // const session = await lucia.createSession(auth.id, {}); + // const session = await lucia.createSession(auth.id, {}); - // ctx.res.appendHeader( - // "Set-Cookie", - // lucia.createSessionCookie(session.id).serialize(), - // ); + // ctx.res.appendHeader( + // "Set-Cookie", + // lucia.createSessionCookie(session.id).serialize(), + // ); - return true; - }), + return true; + }), disable2FA: protectedProcedure.mutation(async ({ ctx }) => { // const auth = await findAuthById(ctx.user.authId); // await updateAuthById(auth.id, { diff --git a/packages/server/src/db/schema/admin.ts b/packages/server/src/db/schema/admin.ts deleted file mode 100644 index 983f99fd..00000000 --- a/packages/server/src/db/schema/admin.ts +++ /dev/null @@ -1,210 +0,0 @@ -import { relations } from "drizzle-orm"; -import { - boolean, - integer, - json, - jsonb, - pgTable, - text, -} from "drizzle-orm/pg-core"; -import { createInsertSchema } from "drizzle-zod"; -import { nanoid } from "nanoid"; -import { z } from "zod"; -import { auth } from "./auth"; -import { certificates } from "./certificate"; -import { registry } from "./registry"; -import { certificateType } from "./shared"; -import { sshKeys } from "./ssh-key"; -import { users } from "./user"; - -export const admins = pgTable("admin", { - adminId: text("adminId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - serverIp: text("serverIp"), - certificateType: certificateType("certificateType").notNull().default("none"), - host: text("host"), - letsEncryptEmail: text("letsEncryptEmail"), - sshPrivateKey: text("sshPrivateKey"), - enableDockerCleanup: boolean("enableDockerCleanup").notNull().default(false), - enableLogRotation: boolean("enableLogRotation").notNull().default(false), - authId: text("authId") - .notNull() - .references(() => auth.id, { onDelete: "cascade" }), - createdAt: text("createdAt") - .notNull() - .$defaultFn(() => new Date().toISOString()), - stripeCustomerId: text("stripeCustomerId"), - stripeSubscriptionId: text("stripeSubscriptionId"), - serversQuantity: integer("serversQuantity").notNull().default(0), - - // Metrics - enablePaidFeatures: boolean("enablePaidFeatures").notNull().default(false), - metricsConfig: jsonb("metricsConfig") - .$type<{ - server: { - type: "Dokploy" | "Remote"; - refreshRate: number; - port: number; - token: string; - urlCallback: string; - retentionDays: number; - cronJob: string; - thresholds: { - cpu: number; - memory: number; - }; - }; - containers: { - refreshRate: number; - services: { - include: string[]; - exclude: string[]; - }; - }; - }>() - .notNull() - .default({ - server: { - type: "Dokploy", - refreshRate: 60, - port: 4500, - token: "", - retentionDays: 2, - cronJob: "", - urlCallback: "", - thresholds: { - cpu: 0, - memory: 0, - }, - }, - containers: { - refreshRate: 60, - services: { - include: [], - exclude: [], - }, - }, - }), - cleanupCacheApplications: boolean("cleanupCacheApplications") - .notNull() - .default(false), - cleanupCacheOnPreviews: boolean("cleanupCacheOnPreviews") - .notNull() - .default(false), - cleanupCacheOnCompose: boolean("cleanupCacheOnCompose") - .notNull() - .default(false), -}); - -export const adminsRelations = relations(admins, ({ one, many }) => ({ - auth: one(auth, { - fields: [admins.authId], - references: [auth.id], - }), - users: many(users), - registry: many(registry), - sshKeys: many(sshKeys), - certificates: many(certificates), -})); - -const createSchema = createInsertSchema(admins, { - adminId: z.string(), - enableDockerCleanup: z.boolean().optional(), - sshPrivateKey: z.string().optional(), - certificateType: z.enum(["letsencrypt", "none"]).default("none"), - serverIp: z.string().optional(), - letsEncryptEmail: z.string().optional(), -}); - -export const apiUpdateAdmin = createSchema.partial(); - -export const apiSaveSSHKey = createSchema - .pick({ - sshPrivateKey: true, - }) - .required(); - -export const apiAssignDomain = createSchema - .pick({ - host: true, - certificateType: true, - letsEncryptEmail: true, - }) - .required() - .partial({ - letsEncryptEmail: true, - }); - -export const apiUpdateDockerCleanup = createSchema - .pick({ - enableDockerCleanup: true, - }) - .required() - .extend({ - serverId: z.string().optional(), - }); - -export const apiTraefikConfig = z.object({ - traefikConfig: z.string().min(1), -}); - -export const apiModifyTraefikConfig = z.object({ - path: z.string().min(1), - traefikConfig: z.string().min(1), - serverId: z.string().optional(), -}); -export const apiReadTraefikConfig = z.object({ - path: z.string().min(1), - serverId: z.string().optional(), -}); - -export const apiEnableDashboard = z.object({ - enableDashboard: z.boolean().optional(), - serverId: z.string().optional(), -}); - -export const apiServerSchema = z - .object({ - serverId: z.string().optional(), - }) - .optional(); - -export const apiReadStatsLogs = z.object({ - page: z - .object({ - pageIndex: z.number(), - pageSize: z.number(), - }) - .optional(), - status: z.string().array().optional(), - search: z.string().optional(), - sort: z.object({ id: z.string(), desc: z.boolean() }).optional(), -}); - -export const apiUpdateWebServerMonitoring = z.object({ - metricsConfig: z - .object({ - server: z.object({ - refreshRate: z.number().min(2), - port: z.number().min(1), - token: z.string(), - urlCallback: z.string().url(), - retentionDays: z.number().min(1), - cronJob: z.string().min(1), - thresholds: z.object({ - cpu: z.number().min(0), - memory: z.number().min(0), - }), - }), - containers: z.object({ - refreshRate: z.number().min(2), - services: z.object({ - include: z.array(z.string()).optional(), - exclude: z.array(z.string()).optional(), - }), - }), - }) - .required(), -}); diff --git a/packages/server/src/db/schema/auth.ts b/packages/server/src/db/schema/auth.ts deleted file mode 100644 index 7093a40c..00000000 --- a/packages/server/src/db/schema/auth.ts +++ /dev/null @@ -1,130 +0,0 @@ -import { getRandomValues } from "node:crypto"; -import { relations } from "drizzle-orm"; -import { boolean, pgEnum, pgTable, text } from "drizzle-orm/pg-core"; -import { createInsertSchema } from "drizzle-zod"; -import { nanoid } from "nanoid"; -import { z } from "zod"; -// import { admins } from "./admin"; -import { users } from "./user"; - -const randomImages = [ - "/avatars/avatar-1.png", - "/avatars/avatar-2.png", - "/avatars/avatar-3.png", - "/avatars/avatar-4.png", - "/avatars/avatar-5.png", - "/avatars/avatar-6.png", - "/avatars/avatar-7.png", - "/avatars/avatar-8.png", - "/avatars/avatar-9.png", - "/avatars/avatar-10.png", - "/avatars/avatar-11.png", - "/avatars/avatar-12.png", -]; - -const generateRandomImage = () => { - return ( - randomImages[ - // @ts-ignore - getRandomValues(new Uint32Array(1))[0] % randomImages.length - ] || "/avatars/avatar-1.png" - ); -}; -export type DatabaseUser = typeof auth.$inferSelect; -export const roles = pgEnum("Roles", ["admin", "user"]); - -export const auth = pgTable("auth", { - id: text("id") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - email: text("email").notNull().unique(), - password: text("password").notNull(), - rol: roles("rol").notNull(), - image: text("image").$defaultFn(() => generateRandomImage()), - secret: text("secret"), - token: text("token"), - is2FAEnabled: boolean("is2FAEnabled").notNull().default(false), - createdAt: text("createdAt") - .notNull() - .$defaultFn(() => new Date().toISOString()), - resetPasswordToken: text("resetPasswordToken"), - resetPasswordExpiresAt: text("resetPasswordExpiresAt"), - confirmationToken: text("confirmationToken"), - confirmationExpiresAt: text("confirmationExpiresAt"), -}); - -export const authRelations = relations(auth, ({ many }) => ({ - // admins: many(admins), - users: many(users), -})); -const createSchema = createInsertSchema(auth, { - email: z.string().email(), - password: z.string().min(8), - rol: z.enum(["admin", "user"]), - image: z.string().optional(), -}); - -export const apiCreateAdmin = createSchema.pick({ - email: true, - password: true, -}); - -export const apiCreateUser = createSchema - .pick({ - password: true, - id: true, - token: true, - }) - .required() - .extend({ - token: z.string().min(1), - }); - -export const apiLogin = createSchema - .pick({ - email: true, - password: true, - }) - .required(); - -export const apiUpdateAuth = createSchema.partial().extend({ - email: z.string().nullable(), - password: z.string().nullable(), - image: z.string().optional(), - currentPassword: z.string().nullable(), -}); - -export const apiUpdateAuthByAdmin = createSchema.partial().extend({ - email: z.string().nullable(), - password: z.string().nullable(), - image: z.string().optional(), - id: z.string().min(1), -}); - -export const apiFindOneAuth = createSchema - .pick({ - id: true, - }) - .required(); - -export const apiVerify2FA = createSchema - .extend({ - pin: z.string().min(6), - secret: z.string().min(1), - }) - .pick({ - pin: true, - secret: true, - }) - .required(); - -export const apiVerifyLogin2FA = createSchema - .extend({ - pin: z.string().min(6), - }) - .pick({ - pin: true, - id: true, - }) - .required(); diff --git a/packages/server/src/db/schema/certificate.ts b/packages/server/src/db/schema/certificate.ts index dd121b50..80d53350 100644 --- a/packages/server/src/db/schema/certificate.ts +++ b/packages/server/src/db/schema/certificate.ts @@ -4,10 +4,7 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { admins } from "./admin"; import { server } from "./server"; -import { users_temp } from "./user"; -// import { user } from "./user"; import { generateAppName } from "./utils"; export const certificates = pgTable("certificate", { diff --git a/packages/server/src/db/schema/deployment.ts b/packages/server/src/db/schema/deployment.ts index 1be5db5e..4dfed76b 100644 --- a/packages/server/src/db/schema/deployment.ts +++ b/packages/server/src/db/schema/deployment.ts @@ -1,4 +1,4 @@ -import { is, relations } from "drizzle-orm"; +import { relations } from "drizzle-orm"; import { type AnyPgColumn, boolean, diff --git a/packages/server/src/db/schema/destination.ts b/packages/server/src/db/schema/destination.ts index 4d8dbc13..0aeb1490 100644 --- a/packages/server/src/db/schema/destination.ts +++ b/packages/server/src/db/schema/destination.ts @@ -4,10 +4,7 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { admins } from "./admin"; import { backups } from "./backups"; -import { users_temp } from "./user"; -// import { user } from "./user"; export const destinations = pgTable("destination", { destinationId: text("destinationId") @@ -20,7 +17,6 @@ export const destinations = pgTable("destination", { secretAccessKey: text("secretAccessKey").notNull(), bucket: text("bucket").notNull(), region: text("region").notNull(), - // maybe it can be null endpoint: text("endpoint").notNull(), organizationId: text("organizationId") .notNull() @@ -35,10 +31,6 @@ export const destinationsRelations = relations( fields: [destinations.organizationId], references: [organization.id], }), - // user: one(user, { - // fields: [destinations.userId], - // references: [user.id], - // }), }), ); diff --git a/packages/server/src/db/schema/git-provider.ts b/packages/server/src/db/schema/git-provider.ts index e4255714..dc88131a 100644 --- a/packages/server/src/db/schema/git-provider.ts +++ b/packages/server/src/db/schema/git-provider.ts @@ -4,12 +4,9 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { admins } from "./admin"; import { bitbucket } from "./bitbucket"; import { github } from "./github"; import { gitlab } from "./gitlab"; -import { users_temp } from "./user"; -// import { user } from "./user"; export const gitProviderType = pgEnum("gitProviderType", [ "github", diff --git a/packages/server/src/db/schema/index.ts b/packages/server/src/db/schema/index.ts index 405fa383..5eb7f369 100644 --- a/packages/server/src/db/schema/index.ts +++ b/packages/server/src/db/schema/index.ts @@ -1,8 +1,6 @@ export * from "./application"; export * from "./postgres"; export * from "./user"; -export * from "./admin"; -export * from "./auth"; export * from "./project"; export * from "./domain"; export * from "./mariadb"; diff --git a/packages/server/src/db/schema/notification.ts b/packages/server/src/db/schema/notification.ts index 0179f3c7..4adce37d 100644 --- a/packages/server/src/db/schema/notification.ts +++ b/packages/server/src/db/schema/notification.ts @@ -4,8 +4,6 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { users_temp } from "./user"; -// import { user } from "./user"; export const notificationType = pgEnum("notificationType", [ "slack", diff --git a/packages/server/src/db/schema/project.ts b/packages/server/src/db/schema/project.ts index cf4ea8a8..deeba4ac 100644 --- a/packages/server/src/db/schema/project.ts +++ b/packages/server/src/db/schema/project.ts @@ -1,12 +1,9 @@ import { relations } from "drizzle-orm"; - import { pgTable, text } from "drizzle-orm/pg-core"; import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { admins } from "./admin"; -// import { admins } from "./admin"; import { applications } from "./application"; import { compose } from "./compose"; import { mariadb } from "./mariadb"; @@ -14,7 +11,6 @@ import { mongo } from "./mongo"; import { mysql } from "./mysql"; import { postgres } from "./postgres"; import { redis } from "./redis"; -import { users, users_temp } from "./user"; export const projects = pgTable("project", { projectId: text("projectId") diff --git a/packages/server/src/db/schema/registry.ts b/packages/server/src/db/schema/registry.ts index aa362a05..35526f90 100644 --- a/packages/server/src/db/schema/registry.ts +++ b/packages/server/src/db/schema/registry.ts @@ -4,10 +4,7 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { organization } from "./account"; -import { admins } from "./admin"; import { applications } from "./application"; -import { users_temp } from "./user"; -// import { user } from "./user"; /** * This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same * database instance for multiple projects. @@ -36,10 +33,6 @@ export const registry = pgTable("registry", { }); export const registryRelations = relations(registry, ({ one, many }) => ({ - // user: one(user, { - // fields: [registry.userId], - // references: [user.id], - // }), applications: many(applications), })); diff --git a/packages/server/src/db/schema/server.ts b/packages/server/src/db/schema/server.ts index c94fd693..26bb4632 100644 --- a/packages/server/src/db/schema/server.ts +++ b/packages/server/src/db/schema/server.ts @@ -10,9 +10,7 @@ import { import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; - import { organization } from "./account"; -import { admins } from "./admin"; import { applications } from "./application"; import { certificates } from "./certificate"; import { compose } from "./compose"; @@ -23,8 +21,6 @@ import { mysql } from "./mysql"; import { postgres } from "./postgres"; import { redis } from "./redis"; import { sshKeys } from "./ssh-key"; -import { users_temp } from "./user"; -// import { user } from "./user"; import { generateAppName } from "./utils"; export const serverStatus = pgEnum("serverStatus", ["active", "inactive"]); @@ -101,10 +97,6 @@ export const server = pgTable("server", { }); export const serverRelations = relations(server, ({ one, many }) => ({ - // user: one(user, { - // fields: [server.userId], - // references: [user.id], - // }), deployments: many(deployments), sshKey: one(sshKeys, { fields: [server.sshKeyId], diff --git a/packages/server/src/db/schema/session.ts b/packages/server/src/db/schema/session.ts index 4e9d2883..99df9218 100644 --- a/packages/server/src/db/schema/session.ts +++ b/packages/server/src/db/schema/session.ts @@ -1,6 +1,4 @@ -import { sql } from "drizzle-orm"; import { pgTable, text, timestamp } from "drizzle-orm/pg-core"; -import { auth } from "./auth"; import { users_temp } from "./user"; // OLD TABLE @@ -18,14 +16,3 @@ export const session = pgTable("session_temp", { impersonatedBy: text("impersonated_by"), activeOrganizationId: text("active_organization_id"), }); - -export const sessionTable = pgTable("session", { - id: text("id").primaryKey(), - userId: text("user_id") - .notNull() - .references(() => auth.id, { onDelete: "cascade" }), - expiresAt: timestamp("expires_at", { - withTimezone: true, - mode: "date", - }).notNull(), -}); diff --git a/packages/server/src/db/schema/source.ts b/packages/server/src/db/schema/source.ts deleted file mode 100644 index 6618ced7..00000000 --- a/packages/server/src/db/schema/source.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { pgTable, text } from "drizzle-orm/pg-core"; -import { createInsertSchema } from "drizzle-zod"; -import { nanoid } from "nanoid"; -import { z } from "zod"; - -export const source = pgTable("project", { - projectId: text("projectId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - name: text("name").notNull(), - description: text("description"), - createdAt: text("createdAt") - .notNull() - .$defaultFn(() => new Date().toISOString()), -}); - -const createSchema = createInsertSchema(source, { - name: z.string().min(1), - description: z.string(), - projectId: z.string(), -}); - -export const apiCreate = createSchema.pick({ - name: true, - description: true, -}); diff --git a/packages/server/src/db/schema/ssh-key.ts b/packages/server/src/db/schema/ssh-key.ts index b705be36..8a66d6d9 100644 --- a/packages/server/src/db/schema/ssh-key.ts +++ b/packages/server/src/db/schema/ssh-key.ts @@ -4,12 +4,9 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { sshKeyCreate, sshKeyType } from "../validations"; import { organization } from "./account"; -import { admins } from "./admin"; import { applications } from "./application"; import { compose } from "./compose"; import { server } from "./server"; -import { users_temp } from "./user"; -// import { user } from "./user"; export const sshKeys = pgTable("ssh-key", { sshKeyId: text("sshKeyId") diff --git a/packages/server/src/db/schema/user.ts b/packages/server/src/db/schema/user.ts index e2755b8f..33e9e4fc 100644 --- a/packages/server/src/db/schema/user.ts +++ b/packages/server/src/db/schema/user.ts @@ -11,8 +11,6 @@ import { createInsertSchema } from "drizzle-zod"; import { nanoid } from "nanoid"; import { z } from "zod"; import { account, organization } from "./account"; -import { admins } from "./admin"; -import { auth } from "./auth"; import { projects } from "./project"; import { certificateType } from "./shared"; /** @@ -24,50 +22,6 @@ import { certificateType } from "./shared"; // OLD TABLE -export const users = pgTable("user", { - userId: text("userId") - .notNull() - .primaryKey() - .$defaultFn(() => nanoid()), - - token: text("token").notNull(), - isRegistered: boolean("isRegistered").notNull().default(false), - expirationDate: timestamp("expirationDate", { - precision: 3, - mode: "string", - }).notNull(), - createdAt: text("createdAt") - .notNull() - .$defaultFn(() => new Date().toISOString()), - canCreateProjects: boolean("canCreateProjects").notNull().default(false), - canAccessToSSHKeys: boolean("canAccessToSSHKeys").notNull().default(false), - canCreateServices: boolean("canCreateServices").notNull().default(false), - canDeleteProjects: boolean("canDeleteProjects").notNull().default(false), - canDeleteServices: boolean("canDeleteServices").notNull().default(false), - canAccessToDocker: boolean("canAccessToDocker").notNull().default(false), - canAccessToAPI: boolean("canAccessToAPI").notNull().default(false), - canAccessToGitProviders: boolean("canAccessToGitProviders") - .notNull() - .default(false), - canAccessToTraefikFiles: boolean("canAccessToTraefikFiles") - .notNull() - .default(false), - accessedProjects: text("accesedProjects") - .array() - .notNull() - .default(sql`ARRAY[]::text[]`), - accessedServices: text("accesedServices") - .array() - .notNull() - .default(sql`ARRAY[]::text[]`), - adminId: text("adminId") - .notNull() - .references(() => admins.adminId, { onDelete: "cascade" }), - authId: text("authId") - .notNull() - .references(() => auth.id, { onDelete: "cascade" }), -}); - // TEMP export const users_temp = pgTable("user_temp", { id: text("id") @@ -187,19 +141,11 @@ export const users_temp = pgTable("user_temp", { }); export const usersRelations = relations(users_temp, ({ one, many }) => ({ - // auth: one(auth, { - // fields: [users.authId], - // references: [auth.id], - // }), account: one(account, { fields: [users_temp.id], references: [account.userId], }), organizations: many(organization), - // admin: one(admins, { - // fields: [users.adminId], - // references: [admins.adminId], - // }), projects: many(projects), })); @@ -263,3 +209,91 @@ export const apiFindOneUserByAuth = createSchema // authId: true, }) .required(); +export const apiSaveSSHKey = createSchema + .pick({ + sshPrivateKey: true, + }) + .required(); + +export const apiAssignDomain = createSchema + .pick({ + host: true, + certificateType: true, + letsEncryptEmail: true, + }) + .required() + .partial({ + letsEncryptEmail: true, + }); + +export const apiUpdateDockerCleanup = createSchema + .pick({ + enableDockerCleanup: true, + }) + .required() + .extend({ + serverId: z.string().optional(), + }); + +export const apiTraefikConfig = z.object({ + traefikConfig: z.string().min(1), +}); + +export const apiModifyTraefikConfig = z.object({ + path: z.string().min(1), + traefikConfig: z.string().min(1), + serverId: z.string().optional(), +}); +export const apiReadTraefikConfig = z.object({ + path: z.string().min(1), + serverId: z.string().optional(), +}); + +export const apiEnableDashboard = z.object({ + enableDashboard: z.boolean().optional(), + serverId: z.string().optional(), +}); + +export const apiServerSchema = z + .object({ + serverId: z.string().optional(), + }) + .optional(); + +export const apiReadStatsLogs = z.object({ + page: z + .object({ + pageIndex: z.number(), + pageSize: z.number(), + }) + .optional(), + status: z.string().array().optional(), + search: z.string().optional(), + sort: z.object({ id: z.string(), desc: z.boolean() }).optional(), +}); + +export const apiUpdateWebServerMonitoring = z.object({ + metricsConfig: z + .object({ + server: z.object({ + refreshRate: z.number().min(2), + port: z.number().min(1), + token: z.string(), + urlCallback: z.string().url(), + retentionDays: z.number().min(1), + cronJob: z.string().min(1), + thresholds: z.object({ + cpu: z.number().min(0), + memory: z.number().min(0), + }), + }), + containers: z.object({ + refreshRate: z.number().min(2), + services: z.object({ + include: z.array(z.string()).optional(), + exclude: z.array(z.string()).optional(), + }), + }), + }) + .required(), +}); diff --git a/packages/server/src/services/admin.ts b/packages/server/src/services/admin.ts index 41b92587..4e1b1bb3 100644 --- a/packages/server/src/services/admin.ts +++ b/packages/server/src/services/admin.ts @@ -2,9 +2,7 @@ import { randomBytes } from "node:crypto"; import { db } from "@dokploy/server/db"; import { account, - admins, type apiCreateUserInvitation, - auth, member, organization, users_temp, diff --git a/packages/server/src/services/auth.ts b/packages/server/src/services/auth.ts index 74cd0419..dbdf538b 100644 --- a/packages/server/src/services/auth.ts +++ b/packages/server/src/services/auth.ts @@ -1,12 +1,6 @@ import { randomBytes } from "node:crypto"; import { db } from "@dokploy/server/db"; -import { - admins, - type apiCreateAdmin, - type apiCreateUser, - auth, - users_temp, -} from "@dokploy/server/db/schema"; +import { users_temp } from "@dokploy/server/db/schema"; import { getPublicIpWithFallback } from "@dokploy/server/wss/utils"; import { TRPCError } from "@trpc/server"; import * as bcrypt from "bcrypt"; @@ -17,89 +11,6 @@ import QRCode from "qrcode"; import { IS_CLOUD } from "../constants"; import { findUserById } from "./admin"; -export type Auth = typeof auth.$inferSelect; - -export const createAdmin = async (input: typeof apiCreateAdmin._type) => { - return await db.transaction(async (tx) => { - const hashedPassword = bcrypt.hashSync(input.password, 10); - const newAuth = await tx - .insert(auth) - .values({ - email: input.email.toLowerCase(), - password: hashedPassword, - rol: "admin", - }) - .returning() - .then((res) => res[0]); - - if (!newAuth) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the user", - }); - } - - await tx - .insert(admins) - .values({ - authId: newAuth.id, - ...(!IS_CLOUD && { - serverIp: - process.env.ADVERTISE_ADDR || (await getPublicIpWithFallback()), - }), - }) - .returning(); - - return newAuth; - }); -}; - -export const createUser = async (input: typeof apiCreateUser._type) => { - return await db.transaction(async (tx) => { - const hashedPassword = bcrypt.hashSync(input.password, 10); - const res = await tx - .update(auth) - .set({ - password: hashedPassword, - }) - .where(eq(auth.id, input.id)) - .returning() - .then((res) => res[0]); - - if (!res) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Error creating the user", - }); - } - - const user = await tx - .update(users) - .set({ - isRegistered: true, - expirationDate: undefined, - }) - .where(eq(users.token, input.token)) - .returning() - .then((res) => res[0]); - - return user; - }); -}; - -export const findAuthByEmail = async (email: string) => { - const result = await db.query.auth.findFirst({ - where: eq(auth.email, email), - }); - if (!result) { - throw new TRPCError({ - code: "NOT_FOUND", - message: "User not found", - }); - } - return result; -}; - export const findAuthById = async (authId: string) => { const result = await db.query.users_temp.findFirst({ where: eq(users_temp.id, authId), @@ -117,21 +28,6 @@ export const findAuthById = async (authId: string) => { return result; }; -export const updateAuthById = async ( - authId: string, - authData: Partial, -) => { - const result = await db - .update(auth) - .set({ - ...authData, - }) - .where(eq(auth.id, authId)) - .returning(); - - return result[0]; -}; - export const generate2FASecret = async (userId: string) => { const user = await findUserById(userId);