mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Refactor password validation (#16902)
* ui: add common constant PASSWORD_MIN_LENGTH=8 * ui: simplify password validation logic * ui: getPasswordStrengthLabel using t from context Co-authored-by: Alex Donesky <adonesky@gmail.com>
This commit is contained in:
parent
4ba081a096
commit
1acb1b2e5d
@ -6,6 +6,7 @@ import Button from '../../ui/button';
|
|||||||
import CheckBox from '../../ui/check-box';
|
import CheckBox from '../../ui/check-box';
|
||||||
import Typography from '../../ui/typography';
|
import Typography from '../../ui/typography';
|
||||||
import SrpInput from '../srp-input';
|
import SrpInput from '../srp-input';
|
||||||
|
import { PASSWORD_MIN_LENGTH } from '../../../helpers/constants/common';
|
||||||
|
|
||||||
export default function CreateNewVault({
|
export default function CreateNewVault({
|
||||||
disabled = false,
|
disabled = false,
|
||||||
@ -27,7 +28,7 @@ export default function CreateNewVault({
|
|||||||
let newConfirmPasswordError = '';
|
let newConfirmPasswordError = '';
|
||||||
let newPasswordError = '';
|
let newPasswordError = '';
|
||||||
|
|
||||||
if (newPassword && newPassword.length < 8) {
|
if (newPassword && newPassword.length < PASSWORD_MIN_LENGTH) {
|
||||||
newPasswordError = t('passwordNotLongEnough');
|
newPasswordError = t('passwordNotLongEnough');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,3 +23,4 @@ _supportRequestLink =
|
|||||||
|
|
||||||
export const SUPPORT_REQUEST_LINK = _supportRequestLink;
|
export const SUPPORT_REQUEST_LINK = _supportRequestLink;
|
||||||
export const CONTRACT_ADDRESS_LINK = _contractAddressLink;
|
export const CONTRACT_ADDRESS_LINK = _contractAddressLink;
|
||||||
|
export const PASSWORD_MIN_LENGTH = 8;
|
||||||
|
@ -26,6 +26,7 @@ import {
|
|||||||
TwoStepProgressBar,
|
TwoStepProgressBar,
|
||||||
twoStepStages,
|
twoStepStages,
|
||||||
} from '../../../components/app/step-progress-bar';
|
} from '../../../components/app/step-progress-bar';
|
||||||
|
import { PASSWORD_MIN_LENGTH } from '../../../helpers/constants/common';
|
||||||
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
|
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
|
||||||
import { getFirstTimeFlowType } from '../../../selectors';
|
import { getFirstTimeFlowType } from '../../../selectors';
|
||||||
import { FIRST_TIME_FLOW_TYPES } from '../../../helpers/constants/onboarding';
|
import { FIRST_TIME_FLOW_TYPES } from '../../../helpers/constants/onboarding';
|
||||||
@ -55,76 +56,66 @@ export default function CreatePassword({
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (password.length < 8) {
|
if (password.length < PASSWORD_MIN_LENGTH) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return !passwordError && !confirmPasswordError;
|
return !passwordError && !confirmPasswordError;
|
||||||
}, [password, confirmPassword, passwordError, confirmPasswordError]);
|
}, [password, confirmPassword, passwordError, confirmPasswordError]);
|
||||||
|
|
||||||
const getPasswordStrengthLabel = (score, translation) => {
|
const getPasswordStrengthLabel = (isTooShort, score) => {
|
||||||
|
if (isTooShort) {
|
||||||
|
return {
|
||||||
|
className: 'create-password__weak',
|
||||||
|
text: t('passwordNotLongEnough'),
|
||||||
|
description: '',
|
||||||
|
};
|
||||||
|
}
|
||||||
if (score >= 4) {
|
if (score >= 4) {
|
||||||
return {
|
return {
|
||||||
className: 'create-password__strong',
|
className: 'create-password__strong',
|
||||||
text: translation('strong'),
|
text: t('strong'),
|
||||||
description: '',
|
description: '',
|
||||||
};
|
};
|
||||||
} else if (score === 3) {
|
}
|
||||||
|
if (score === 3) {
|
||||||
return {
|
return {
|
||||||
className: 'create-password__average',
|
className: 'create-password__average',
|
||||||
text: translation('average'),
|
text: t('average'),
|
||||||
description: t('passwordStrengthDescription'),
|
description: t('passwordStrengthDescription'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
className: 'create-password__weak',
|
className: 'create-password__weak',
|
||||||
text: translation('weak'),
|
text: t('weak'),
|
||||||
description: t('passwordStrengthDescription'),
|
description: t('passwordStrengthDescription'),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePasswordChange = (passwordInput) => {
|
const handlePasswordChange = (passwordInput) => {
|
||||||
let confirmError = '';
|
const isTooShort =
|
||||||
let passwordInputError = '';
|
passwordInput.length && passwordInput.length < PASSWORD_MIN_LENGTH;
|
||||||
const passwordEvaluation = zxcvbn(passwordInput);
|
const { score } = zxcvbn(passwordInput);
|
||||||
const passwordStrengthLabel = getPasswordStrengthLabel(
|
const passwordStrengthLabel = getPasswordStrengthLabel(isTooShort, score);
|
||||||
passwordEvaluation.score,
|
const passwordStrengthComponent = t('passwordStrength', [
|
||||||
t,
|
<span key={score} className={passwordStrengthLabel.className}>
|
||||||
);
|
|
||||||
let passwordStrengthDescription = passwordStrengthLabel.description;
|
|
||||||
let passwordStrengthInput = t('passwordStrength', [
|
|
||||||
<span
|
|
||||||
key={passwordEvaluation.score}
|
|
||||||
className={passwordStrengthLabel.className}
|
|
||||||
>
|
|
||||||
{passwordStrengthLabel.text}
|
{passwordStrengthLabel.text}
|
||||||
</span>,
|
</span>,
|
||||||
]);
|
]);
|
||||||
|
const confirmError =
|
||||||
if (passwordInput.length < 8) {
|
!confirmPassword || passwordInput === confirmPassword
|
||||||
passwordInputError = passwordInput.length
|
? ''
|
||||||
? t('passwordNotLongEnough')
|
: t('passwordsDontMatch');
|
||||||
: '';
|
|
||||||
passwordStrengthInput = null;
|
|
||||||
passwordStrengthDescription = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (confirmPassword && passwordInput !== confirmPassword) {
|
|
||||||
confirmError = t('passwordsDontMatch');
|
|
||||||
}
|
|
||||||
|
|
||||||
setPassword(passwordInput);
|
setPassword(passwordInput);
|
||||||
setPasswordError(passwordInputError);
|
setPasswordStrength(passwordStrengthComponent);
|
||||||
setPasswordStrength(passwordStrengthInput);
|
setPasswordStrengthText(passwordStrengthLabel.description);
|
||||||
setPasswordStrengthText(passwordStrengthDescription);
|
|
||||||
setConfirmPasswordError(confirmError);
|
setConfirmPasswordError(confirmError);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleConfirmPasswordChange = (confirmPasswordInput) => {
|
const handleConfirmPasswordChange = (confirmPasswordInput) => {
|
||||||
let error = '';
|
const error =
|
||||||
if (password !== confirmPasswordInput) {
|
password === confirmPasswordInput ? '' : t('passwordsDontMatch');
|
||||||
error = t('passwordsDontMatch');
|
|
||||||
}
|
|
||||||
|
|
||||||
setConfirmPassword(confirmPasswordInput);
|
setConfirmPassword(confirmPasswordInput);
|
||||||
setConfirmPasswordError(error);
|
setConfirmPasswordError(error);
|
||||||
@ -185,7 +176,6 @@ export default function CreatePassword({
|
|||||||
<FormField
|
<FormField
|
||||||
dataTestId="create-password-new"
|
dataTestId="create-password-new"
|
||||||
autoFocus
|
autoFocus
|
||||||
error={passwordError}
|
|
||||||
passwordStrength={passwordStrength}
|
passwordStrength={passwordStrength}
|
||||||
passwordStrengthText={passwordStrengthText}
|
passwordStrengthText={passwordStrengthText}
|
||||||
onChange={handlePasswordChange}
|
onChange={handlePasswordChange}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user