From b7874f053f87e0b48c44a1ae103d0bee3ac8c653 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 23 Mar 2025 04:55:16 -0600 Subject: [PATCH] feat(licenses): enhance license validation process - Updated the validateLicense function to accept server IP for improved validation. - Modified user API to utilize the new validateLicense signature. - Added useEffect in EnablePaidFeatures component to set license key based on user data. - Improved error handling and user feedback during license validation. --- .../settings/enable-paid-features.tsx | 15 +++++++++++++-- apps/dokploy/server/api/routers/user.ts | 10 +++++++--- apps/dokploy/server/utils/validate-license.ts | 19 ++++++++++++++++--- apps/licenses/src/index.ts | 2 ++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx b/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx index cde891d2b..6b704029c 100644 --- a/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx +++ b/apps/dokploy/components/dashboard/settings/enable-paid-features.tsx @@ -12,7 +12,7 @@ import { api } from "@/utils/api"; import { SetupMonitoring } from "./servers/setup-monitoring"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; -import { useState } from "react"; +import { useState, useEffect } from "react"; export const EnablePaidFeatures = () => { const { data, refetch } = api.user.get.useQuery(); @@ -21,14 +21,25 @@ export const EnablePaidFeatures = () => { const { mutateAsync: update } = api.user.update.useMutation(); const [licenseKey, setLicenseKey] = useState(""); + useEffect(() => { + if (data?.user?.enablePaidFeatures) { + setLicenseKey(data.user.licenseKey || ""); + } + }, [data?.user?.enablePaidFeatures]); + const handleValidateLicense = async () => { + if (!licenseKey) { + toast.error("Please enter a license key"); + return; + } await validateLicense({ licenseKey, }) .then(() => { toast.success("License validated successfully"); }) - .catch(() => { + .catch((e) => { + console.error(e); toast.error("Error validating license"); }); }; diff --git a/apps/dokploy/server/api/routers/user.ts b/apps/dokploy/server/api/routers/user.ts index e3014bf70..eb65c1d49 100644 --- a/apps/dokploy/server/api/routers/user.ts +++ b/apps/dokploy/server/api/routers/user.ts @@ -140,14 +140,18 @@ export const userRouter = createTRPCRouter({ } return await updateUser(ctx.user.id, input); }), - validateLicense: protectedProcedure + validateLicense: adminProcedure .input( z.object({ licenseKey: z.string(), }), ) .mutation(async ({ input, ctx }) => { - const isValid = await validateLicense(input.licenseKey); + const owner = await findUserById(ctx.user.ownerId); + const isValid = await validateLicense( + input.licenseKey, + owner?.serverIp || "", + ); if (!isValid) { throw new TRPCError({ code: "UNAUTHORIZED", @@ -363,7 +367,7 @@ export const userRouter = createTRPCRouter({ const user = await findUserById(ctx.user.id); - if (!validateLicense(user?.licenseKey || "")) { + if (!validateLicense(user?.licenseKey || "", user?.serverIp || "")) { throw new TRPCError({ code: "UNAUTHORIZED", message: "Invalid license key", diff --git a/apps/dokploy/server/utils/validate-license.ts b/apps/dokploy/server/utils/validate-license.ts index 748234eb9..8c786b36a 100644 --- a/apps/dokploy/server/utils/validate-license.ts +++ b/apps/dokploy/server/utils/validate-license.ts @@ -1,8 +1,21 @@ -export const validateLicense = async (licenseKey: string): Promise => { - const response = await fetch(`${process.env.SERVER_URL}/validate-license`, { +const licensesUrl = process.env.LICENSES_URL || "http://localhost:4002"; + +export const validateLicense = async ( + licenseKey: string, + serverIp: string, +): Promise => { + const response = await fetch(`${licensesUrl}/api/validate`, { method: "POST", - body: JSON.stringify({ licenseKey }), + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ licenseKey, serverIp }), }); + const data = await response.json(); + + if (!response.ok && data.error?.issues) { + console.log("Validation errors:", data.error.issues); + } return response.ok; }; diff --git a/apps/licenses/src/index.ts b/apps/licenses/src/index.ts index adbfef33c..3d4939bac 100644 --- a/apps/licenses/src/index.ts +++ b/apps/licenses/src/index.ts @@ -56,6 +56,8 @@ router.get("/health", async (c) => { router.post("/validate", zValidator("json", validateSchema), async (c) => { const { licenseKey, serverIp } = c.req.valid("json"); + console.log("Validating license", licenseKey, serverIp); + try { const result = await validateLicense(licenseKey, serverIp); return c.json(result);