From c900fc0ccc41e32325c594d7d0cc4e20e8edd6ec Mon Sep 17 00:00:00 2001 From: ItzLinaton <162156251+itzlinaton@users.noreply.github.com> Date: Mon, 2 Nov 2026 01:50:22 +0100 Subject: [PATCH] Feat: Random password generator (V2) (#7272) Added a secure password generator to admin customer user manager and user account manager as shown below: ## Extra Details: - Added a reusable password field component with: - 17-character random password generation - Uppercase and lowercase letters - Numbers - Symbols - Password visibility toggle button - Clipboard copy button after the new password is generated - Integrated the feature into: - Admin customer user creation area - Admin customer user editing area - Customer User account settings --- .../admin/users/UserDetailContainer.tsx | 3 +- .../components/admin/users/UserFormFields.tsx | 3 +- resources/scripts/components/form/fields.tsx | 112 +++++++++++++++++- resources/scripts/components/form/index.ts | 2 + resources/scripts/lib/passwords.ts | 47 ++++++++ .../classic/views/account/PasswordCard.tsx | 4 +- .../themes/classic/views/shared/fields.tsx | 95 ++++++++++++++- 7 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 resources/scripts/lib/passwords.ts diff --git a/resources/scripts/components/admin/users/UserDetailContainer.tsx b/resources/scripts/components/admin/users/UserDetailContainer.tsx index e35c47770..ee0752fcc 100644 --- a/resources/scripts/components/admin/users/UserDetailContainer.tsx +++ b/resources/scripts/components/admin/users/UserDetailContainer.tsx @@ -140,8 +140,7 @@ const PasswordCard = ({ form }: { form: UserForm }) => ( {(field) => ( - {(field) => ( - , 'name' | 'value' | 'onChange' | 'defaultValue' | 'type'>; + +export function PasswordField({ + id, + label, + description, + light, + onBlur, + className, + 'aria-describedby': ariaDescribedBy, + 'aria-invalid': ariaInvalid, + ...props +}: PasswordFieldProps) { + const { field, error, fieldId, helpId, describedById } = useFieldState(id, description); + const [visible, setVisible] = useState(false); + const [generated, setGenerated] = useState(false); + const toggleLabel = `${visible ? 'Hide' : 'Show'} ${label?.toLowerCase() ?? 'password'}`; + + const generate = () => { + field.handleChange(generateSecurePassword()); + setGenerated(true); + }; + + return ( + + {label && ( + + {label} + + )} + + { + field.handleChange(value); + setGenerated(false); + }} + onBlur={(event) => { + field.handleBlur(); + onBlur?.(event); + }} + aria-invalid={error ? true : ariaInvalid} + aria-describedby={describedBy(ariaDescribedBy, describedById)} + $hasError={!!error} + $isLight={light} + className={cn('pr-24', className)} + /> + + + + + + + + + + + setVisible((current) => !current)} + > + + + + + {generated && ( + + + + + + + + + + )} + + + + + ); +} + export type TextAreaFieldProps = BaseProps & Omit, 'name' | 'value' | 'onChange' | 'defaultValue'>; diff --git a/resources/scripts/components/form/index.ts b/resources/scripts/components/form/index.ts index 896a26cf3..629f2b3c6 100644 --- a/resources/scripts/components/form/index.ts +++ b/resources/scripts/components/form/index.ts @@ -3,6 +3,7 @@ import type { FormAsyncValidateOrFn, FormValidateOrFn } from '@tanstack/form-cor import { fieldContext, formContext } from './context'; import { TextField, + PasswordField, TextAreaField, NumberField, SelectField, @@ -14,6 +15,7 @@ import { SubmitButton } from './SubmitButton'; const fieldComponents = { TextField, + PasswordField, TextAreaField, NumberField, SelectField, diff --git a/resources/scripts/lib/passwords.ts b/resources/scripts/lib/passwords.ts new file mode 100644 index 000000000..01e9e8fa9 --- /dev/null +++ b/resources/scripts/lib/passwords.ts @@ -0,0 +1,47 @@ +const passwordCharacterSets = { + uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + lowercase: 'abcdefghijklmnopqrstuvwxyz', + numbers: '0123456789', + symbols: '!@#$%^&*()_+=-[]{}', +} as const; + +const secureRandomIndex = (length: number): number => { + if (length < 1) { + return 0; + } + + const limit = 0x100000000 - (0x100000000 % length); + const values = new Uint32Array(1); + let value: number; + + do { + crypto.getRandomValues(values); + value = values[0]; + } while (value >= limit); + + return value % length; +}; + +const randomCharacter = (characters: string): string => characters[secureRandomIndex(characters.length)]; + +/** + * Generates a secure password using the defined random character sets. + * + * @param length The requested password length. + */ +export const generateSecurePassword = (length = 17): string => { + const characterSets = Object.values(passwordCharacterSets); + const allCharacters = characterSets.join(''); + const password = characterSets.map(randomCharacter); + + while (password.length < length) { + password.push(randomCharacter(allCharacters)); + } + + for (let index = password.length - 1; index > 0; index--) { + const swapIndex = secureRandomIndex(index + 1); + [password[index], password[swapIndex]] = [password[swapIndex], password[index]]; + } + + return password.join(''); +}; diff --git a/resources/scripts/themes/classic/views/account/PasswordCard.tsx b/resources/scripts/themes/classic/views/account/PasswordCard.tsx index e0e5f4900..3dde9e058 100644 --- a/resources/scripts/themes/classic/views/account/PasswordCard.tsx +++ b/resources/scripts/themes/classic/views/account/PasswordCard.tsx @@ -1,6 +1,6 @@ import { Button, ContentBox, Form, SpinnerOverlay } from '@/sdk'; import type { ViewProps } from '@/sdk/theme'; -import { TextField } from '../shared/fields'; +import { PasswordField, TextField } from '../shared/fields'; export default function PasswordCard({ model, className }: ViewProps<'account.password'>) { const { fields } = model; @@ -11,7 +11,7 @@ export default function PasswordCard({ model, className }: ViewProps<'account.pa - ) => { + field.fieldProps.onChange(event); + setGenerated(false); + }; + + const generate = () => { + const value = generateSecurePassword(); + + field.fieldProps.onChange({ currentTarget: { value } } as ChangeEvent); + setGenerated(true); + }; + + return ( + + {label && ( + + {label} + + )} + + + + + + + + + + + + + setVisible((current) => !current)} + > + + + + + {generated && ( + + + + + + + + + + )} + + + + + ); +} + export function TextAreaField({ field, label,