import { useState } from 'react'; import { Button, Field, LinkButton, Stack } from '@grafana/ui'; import { Form } from 'app/core/components/Form/Form'; import { ValidationLabels, strongPasswordValidations, strongPasswordValidationRegister, } from 'app/core/components/ValidationLabels/ValidationLabels'; import config from 'app/core/config'; import { t, Trans } from 'app/core/internationalization'; import { UserDTO } from 'app/types'; import { PasswordField } from '../../core/components/PasswordField/PasswordField'; import { ChangePasswordFields } from './types'; export interface Props { user: UserDTO; isSaving: boolean; onChangePassword: (payload: ChangePasswordFields) => void; } export const ChangePasswordForm = ({ user, onChangePassword, isSaving }: Props) => { const [displayValidationLabels, setDisplayValidationLabels] = useState(false); const [pristine, setPristine] = useState(true); const { disableLoginForm } = config; const authSource = user.authLabels?.length && user.authLabels[0]; if (authSource === 'LDAP' || authSource === 'Auth Proxy') { return (

You cannot change password when signed in with LDAP or auth proxy.

); } if (authSource && disableLoginForm) { return (

Password cannot be changed here.

); } return (
{({ register, errors, getValues, watch }) => { const newPassword = watch('newPassword'); return ( <> setDisplayValidationLabels(true)} {...register('newPassword', { onBlur: () => setPristine(false), required: t('profile.change-password.new-password-required', 'New password is required'), validate: { strongPasswordValidationRegister, confirm: (v) => v === getValues().confirmNew || t('profile.change-password.passwords-must-match', 'Passwords must match'), old: (v) => v !== getValues().oldPassword || t( 'profile.change-password.new-password-same-as-old', "New password can't be the same as the old one." ), }, })} /> {displayValidationLabels && ( )} v === getValues().newPassword || t('profile.change-password.passwords-must-match', 'Passwords must match'), })} /> Cancel ); }} ); };