diff --git a/apps/dokploy/components/dashboard/project/add-database.tsx b/apps/dokploy/components/dashboard/project/add-database.tsx index 3176b9589..278707cef 100644 --- a/apps/dokploy/components/dashboard/project/add-database.tsx +++ b/apps/dokploy/components/dashboard/project/add-database.tsx @@ -559,6 +559,7 @@ export const AddDatabase = ({ environmentId, projectName }: Props) => { type="password" placeholder="******************" autoComplete="one-time-code" + enablePasswordGenerator={true} {...field} /> @@ -578,6 +579,7 @@ export const AddDatabase = ({ environmentId, projectName }: Props) => { diff --git a/apps/dokploy/components/shared/toggle-visibility-input.tsx b/apps/dokploy/components/shared/toggle-visibility-input.tsx index aea173fbc..660fd753a 100644 --- a/apps/dokploy/components/shared/toggle-visibility-input.tsx +++ b/apps/dokploy/components/shared/toggle-visibility-input.tsx @@ -10,7 +10,7 @@ export const ToggleVisibilityInput = ({ ...props }: InputProps) => { return (
- + )} - + +
)} {errorMessage && ( diff --git a/apps/dokploy/lib/password-utils.ts b/apps/dokploy/lib/password-utils.ts new file mode 100644 index 000000000..b95fd290f --- /dev/null +++ b/apps/dokploy/lib/password-utils.ts @@ -0,0 +1,38 @@ +const DEFAULT_PASSWORD_LENGTH = 20; +const DEFAULT_PASSWORD_CHARSET = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +export const generateRandomPassword = ( + length: number = DEFAULT_PASSWORD_LENGTH, + charset: string = DEFAULT_PASSWORD_CHARSET, +) => { + const safeLength = + Number.isFinite(length) && length > 0 + ? Math.floor(length) + : DEFAULT_PASSWORD_LENGTH; + + if (safeLength <= 0 || charset.length === 0) { + return ""; + } + + const cryptoApi = + typeof globalThis !== "undefined" ? globalThis.crypto : undefined; + + if (!cryptoApi?.getRandomValues) { + let fallback = ""; + for (let i = 0; i < safeLength; i += 1) { + fallback += charset[Math.floor(Math.random() * charset.length)]; + } + return fallback; + } + + const values = new Uint32Array(safeLength); + cryptoApi.getRandomValues(values); + + let result = ""; + for (const value of values) { + result += charset[value % charset.length]; + } + + return result; +};