Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Validate email address in forgot password dialog #6983

Merged
merged 2 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/components/structures/auth/ForgotPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import ServerPicker from "../../views/elements/ServerPicker";
import PassphraseField from '../../views/auth/PassphraseField';
import { replaceableComponent } from "../../../utils/replaceableComponent";
import { PASSWORD_MIN_SCORE } from '../../views/auth/RegistrationForm';

import { IValidationResult } from "../../views/elements/Validation";
import withValidation, { IValidationResult } from "../../views/elements/Validation";
import * as Email from "../../../email";
import InlineSpinner from '../../views/elements/InlineSpinner';

import { logger } from "matrix-js-sdk/src/logger";
Expand Down Expand Up @@ -68,6 +68,7 @@ interface IState {
serverErrorIsFatal: boolean;
serverDeadError: string;

emailFieldValid: boolean;
passwordFieldValid: boolean;
currentHttpRequest?: Promise<any>;
}
Expand All @@ -90,6 +91,7 @@ export default class ForgotPassword extends React.Component<IProps, IState> {
serverIsAlive: true,
serverErrorIsFatal: false,
serverDeadError: "",
emailFieldValid: false,
passwordFieldValid: false,
};

Expand Down Expand Up @@ -169,10 +171,13 @@ export default class ForgotPassword extends React.Component<IProps, IState> {
// refresh the server errors, just in case the server came back online
await this.handleHttpRequest(this.checkServerLiveliness(this.props.serverConfig));

await this['email_field'].validate({ allowEmpty: false });
await this['password_field'].validate({ allowEmpty: false });

if (!this.state.email) {
this.showErrorDialog(_t('The email address linked to your account must be entered.'));
} else if (!this.state.emailFieldValid) {
this.showErrorDialog(_t("The email address doesn't appear to be valid."));
} else if (!this.state.password || !this.state.password2) {
this.showErrorDialog(_t('A new password must be entered.'));
} else if (!this.state.passwordFieldValid) {
Expand Down Expand Up @@ -222,6 +227,32 @@ export default class ForgotPassword extends React.Component<IProps, IState> {
});
}

private validateEmailRules = withValidation({
rules: [
{
key: "required",
test({ value, allowEmpty }) {
return allowEmpty || !!value;
},
invalid: () => _t("Enter email address"),
}, {
key: "email",
test: ({ value }) => !value || Email.looksValid(value),
invalid: () => _t("Doesn't look like a valid email address"),
},
],
});

private onEmailValidate = async (fieldState) => {
const result = await this.validateEmailRules(fieldState);

this.setState({
emailFieldValid: result.valid,
});

return result;
};

private onPasswordValidate(result: IValidationResult) {
this.setState({
passwordFieldValid: result.valid,
Expand Down Expand Up @@ -277,7 +308,9 @@ export default class ForgotPassword extends React.Component<IProps, IState> {
label={_t('Email')}
value={this.state.email}
onChange={this.onInputChanged.bind(this, "email")}
ref={field => this['email_field'] = field}
autoFocus
onValidate={this.onEmailValidate}
onFocus={() => CountlyAnalytics.instance.track("onboarding_forgot_password_email_focus")}
onBlur={() => CountlyAnalytics.instance.track("onboarding_forgot_password_email_blur")}
/>
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3015,6 +3015,7 @@
"Skip verification for now": "Skip verification for now",
"Failed to send email": "Failed to send email",
"The email address linked to your account must be entered.": "The email address linked to your account must be entered.",
"The email address doesn't appear to be valid.": "The email address doesn't appear to be valid.",
"A new password must be entered.": "A new password must be entered.",
"Please choose a strong password": "Please choose a strong password",
"New passwords must match each other.": "New passwords must match each other.",
Expand Down