Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
import { Box, Field, FieldError, FieldGroup, FieldHint, FieldLabel, FieldRow, PasswordInput } from '@rocket.chat/fuselage';
|
|
import { PasswordVerifier, useValidatePassword } from '@rocket.chat/ui-client';
|
|
import { useEndpoint, useToastMessageDispatch } from '@rocket.chat/ui-contexts';
|
|
import type { AllHTMLAttributes } from 'react';
|
|
import { useId } from 'react';
|
|
import { Controller, useFormContext } from 'react-hook-form';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { useAllowPasswordChange } from './useAllowPasswordChange';
|
|
|
|
type PasswordFieldValues = { password: string; confirmationPassword: string };
|
|
|
|
function getAriaDescribedbyForPassword(
|
|
passwordVerifierId: string,
|
|
passwordId: string,
|
|
allowPasswordChange: boolean,
|
|
passwordError: boolean,
|
|
) {
|
|
const error = !allowPasswordChange || passwordError;
|
|
return [passwordVerifierId, !allowPasswordChange && `${passwordId}-hint`, error && `${passwordId}-error`].filter(Boolean).join(' ');
|
|
}
|
|
|
|
const ChangePassword = (props: AllHTMLAttributes<HTMLFormElement>) => {
|
|
const { t } = useTranslation();
|
|
const dispatchToastMessage = useToastMessageDispatch();
|
|
|
|
const passwordId = useId();
|
|
const confirmPasswordId = useId();
|
|
const passwordVerifierId = useId();
|
|
|
|
const {
|
|
watch,
|
|
formState: { errors },
|
|
handleSubmit,
|
|
reset,
|
|
control,
|
|
} = useFormContext<PasswordFieldValues>();
|
|
|
|
const password = watch('password');
|
|
const passwordIsValid = useValidatePassword(password);
|
|
const { allowPasswordChange } = useAllowPasswordChange();
|
|
|
|
const updatePassword = useEndpoint('POST', '/v1/users.updateOwnBasicInfo');
|
|
|
|
const handleSave = async ({ password }: { password?: string }) => {
|
|
try {
|
|
await updatePassword({
|
|
data: {
|
|
newPassword: password,
|
|
},
|
|
});
|
|
dispatchToastMessage({ type: 'success', message: t('Password_changed_successfully') });
|
|
reset();
|
|
} catch (error) {
|
|
dispatchToastMessage({ type: 'error', message: error });
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Box {...props} is='form' autoComplete='off' onSubmit={handleSubmit(handleSave)}>
|
|
<FieldGroup>
|
|
<Field>
|
|
<FieldLabel htmlFor={passwordId}>{t('New_password')}</FieldLabel>
|
|
<FieldRow>
|
|
<Controller
|
|
control={control}
|
|
name='password'
|
|
rules={{
|
|
required: t('Required_field', { field: t('New_password') }),
|
|
validate: () => (password?.length && !passwordIsValid ? t('Password_must_meet_the_complexity_requirements') : true),
|
|
}}
|
|
render={({ field }) => (
|
|
<PasswordInput
|
|
{...field}
|
|
id={passwordId}
|
|
error={errors.password?.message}
|
|
flexGrow={1}
|
|
disabled={!allowPasswordChange}
|
|
aria-describedby={getAriaDescribedbyForPassword(
|
|
passwordVerifierId,
|
|
passwordId,
|
|
!!allowPasswordChange,
|
|
!!errors?.password,
|
|
)}
|
|
aria-invalid={errors.password ? 'true' : 'false'}
|
|
/>
|
|
)}
|
|
/>
|
|
</FieldRow>
|
|
{!allowPasswordChange && <FieldHint id={`${passwordId}-hint`}>{t('Password_Change_Disabled')}</FieldHint>}
|
|
{errors?.password && (
|
|
<FieldError aria-live='assertive' id={`${passwordId}-error`}>
|
|
{errors.password.message}
|
|
</FieldError>
|
|
)}
|
|
{allowPasswordChange && <PasswordVerifier password={password} id={passwordVerifierId} />}
|
|
</Field>
|
|
<Field>
|
|
<FieldLabel htmlFor={confirmPasswordId}>{t('Confirm_password')}</FieldLabel>
|
|
<FieldRow>
|
|
<Controller
|
|
control={control}
|
|
name='confirmationPassword'
|
|
rules={{
|
|
required: t('Required_field', { field: t('Confirm_password') }),
|
|
validate: (confirmationPassword) => (password !== confirmationPassword ? t('Passwords_do_not_match') : true),
|
|
}}
|
|
render={({ field }) => (
|
|
<PasswordInput
|
|
{...field}
|
|
id={confirmPasswordId}
|
|
error={errors.confirmationPassword?.message}
|
|
flexGrow={1}
|
|
disabled={!allowPasswordChange || !passwordIsValid}
|
|
aria-required={password !== '' ? 'true' : 'false'}
|
|
aria-invalid={errors.confirmationPassword ? 'true' : 'false'}
|
|
aria-describedby={errors.confirmationPassword ? `${confirmPasswordId}-error` : undefined}
|
|
/>
|
|
)}
|
|
/>
|
|
</FieldRow>
|
|
{errors.confirmationPassword && (
|
|
<FieldError aria-live='assertive' id={`${confirmPasswordId}-error`}>
|
|
{errors.confirmationPassword.message}
|
|
</FieldError>
|
|
)}
|
|
</Field>
|
|
</FieldGroup>
|
|
</Box>
|
|
);
|
|
};
|
|
|
|
export default ChangePassword;
|