2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 16:59:02 +02:00
|
|
|
import InputField from "./InputField";
|
2018-11-07 10:27:47 +01:00
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
password: string;
|
|
|
|
|
confirmedPassword: string;
|
|
|
|
|
passwordValid: boolean;
|
|
|
|
|
passwordConfirmationFailed: boolean;
|
2018-11-07 10:27:47 +01:00
|
|
|
};
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
passwordChanged: (p1: string, p2: boolean) => void;
|
|
|
|
|
passwordValidator?: (p: string) => boolean;
|
2018-11-07 10:27:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PasswordConfirmation extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2019-10-20 16:59:02 +02:00
|
|
|
password: "",
|
|
|
|
|
confirmedPassword: "",
|
2018-11-07 10:27:47 +01:00
|
|
|
passwordValid: true,
|
2019-10-20 16:59:02 +02:00
|
|
|
passwordConfirmationFailed: false
|
2018-11-07 10:27:47 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.setState({
|
2019-10-20 16:59:02 +02:00
|
|
|
password: "",
|
|
|
|
|
confirmedPassword: "",
|
2018-11-07 10:27:47 +01:00
|
|
|
passwordValid: true,
|
2019-10-20 16:59:02 +02:00
|
|
|
passwordConfirmationFailed: false
|
2018-11-07 10:27:47 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { t } = this.props;
|
|
|
|
|
return (
|
2019-09-19 08:58:49 +02:00
|
|
|
<div className="columns is-multiline">
|
|
|
|
|
<div className="column is-half">
|
|
|
|
|
<InputField
|
2019-10-20 16:59:02 +02:00
|
|
|
label={t("password.newPassword")}
|
2019-09-19 08:58:49 +02:00
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordChange}
|
2019-10-20 16:59:02 +02:00
|
|
|
value={this.state.password ? this.state.password : ""}
|
2019-09-19 08:58:49 +02:00
|
|
|
validationError={!this.state.passwordValid}
|
2019-10-20 16:59:02 +02:00
|
|
|
errorMessage={t("password.passwordInvalid")}
|
2019-09-19 08:58:49 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="column is-half">
|
|
|
|
|
<InputField
|
2019-10-20 16:59:02 +02:00
|
|
|
label={t("password.confirmPassword")}
|
2019-09-19 08:58:49 +02:00
|
|
|
type="password"
|
|
|
|
|
onChange={this.handlePasswordValidationChange}
|
2019-10-20 16:59:02 +02:00
|
|
|
value={this.state ? this.state.confirmedPassword : ""}
|
2019-09-19 08:58:49 +02:00
|
|
|
validationError={this.state.passwordConfirmationFailed}
|
2019-10-20 16:59:02 +02:00
|
|
|
errorMessage={t("password.passwordConfirmFailed")}
|
2019-09-19 08:58:49 +02:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2018-11-07 10:27:47 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
validatePassword = (password: string) => {
|
2018-11-08 13:27:34 +01:00
|
|
|
const { passwordValidator } = this.props;
|
|
|
|
|
if (passwordValidator) {
|
|
|
|
|
return passwordValidator(password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return password.length >= 6 && password.length < 32;
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-07 10:27:47 +01:00
|
|
|
handlePasswordValidationChange = (confirmedPassword: string) => {
|
|
|
|
|
const passwordConfirmed = this.state.password === confirmedPassword;
|
|
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
|
|
|
|
confirmedPassword,
|
2019-10-20 16:59:02 +02:00
|
|
|
passwordConfirmationFailed: !passwordConfirmed
|
2018-11-07 10:27:47 +01:00
|
|
|
},
|
2019-10-20 16:59:02 +02:00
|
|
|
this.propagateChange
|
2018-11-07 10:27:47 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handlePasswordChange = (password: string) => {
|
2019-10-21 10:57:56 +02:00
|
|
|
const passwordConfirmationFailed = password !== this.state.confirmedPassword;
|
2018-11-07 10:27:47 +01:00
|
|
|
|
|
|
|
|
this.setState(
|
|
|
|
|
{
|
2018-11-08 13:27:34 +01:00
|
|
|
passwordValid: this.validatePassword(password),
|
2018-11-07 10:27:47 +01:00
|
|
|
passwordConfirmationFailed,
|
2019-10-20 16:59:02 +02:00
|
|
|
password: password
|
2018-11-07 10:27:47 +01:00
|
|
|
},
|
2019-10-20 16:59:02 +02:00
|
|
|
this.propagateChange
|
2018-11-07 10:27:47 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-06 12:00:30 +01:00
|
|
|
isValid = () => {
|
2019-09-19 08:58:49 +02:00
|
|
|
return this.state.passwordValid && !this.state.passwordConfirmationFailed;
|
2018-12-06 12:00:30 +01:00
|
|
|
};
|
|
|
|
|
|
2018-11-07 10:27:47 +01:00
|
|
|
propagateChange = () => {
|
2018-12-06 12:00:30 +01:00
|
|
|
this.props.passwordChanged(this.state.password, this.isValid());
|
2018-11-07 10:27:47 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
export default withTranslation("commons")(PasswordConfirmation);
|