Make password resets use server config objects

Like registration, the idea is that the object is passed around between components so they can take details they need.
pull/21833/head
Travis Ralston 2019-05-02 23:05:59 -06:00
parent 00ebb5e1fd
commit b6e027f5cb
1 changed files with 23 additions and 65 deletions

View File

@ -21,8 +21,8 @@ import { _t } from '../../../languageHandler';
import sdk from '../../../index'; import sdk from '../../../index';
import Modal from "../../../Modal"; import Modal from "../../../Modal";
import SdkConfig from "../../../SdkConfig"; import SdkConfig from "../../../SdkConfig";
import PasswordReset from "../../../PasswordReset"; import PasswordReset from "../../../PasswordReset";
import {ValidatedServerConfig} from "../../../utils/AutoDiscoveryUtils";
// Phases // Phases
// Show controls to configure server details // Show controls to configure server details
@ -40,28 +40,14 @@ module.exports = React.createClass({
displayName: 'ForgotPassword', displayName: 'ForgotPassword',
propTypes: { propTypes: {
// The default server name to use when the user hasn't specified serverConfig: PropTypes.instanceOf(ValidatedServerConfig).isRequired,
// one. If set, `defaultHsUrl` and `defaultHsUrl` were derived for this onServerConfigChange: PropTypes.func.isRequired,
// via `.well-known` discovery. The server name is used instead of the
// HS URL when talking about "your account".
defaultServerName: PropTypes.string,
// An error passed along from higher up explaining that something
// went wrong when finding the defaultHsUrl.
defaultServerDiscoveryError: PropTypes.string,
defaultHsUrl: PropTypes.string,
defaultIsUrl: PropTypes.string,
customHsUrl: PropTypes.string,
customIsUrl: PropTypes.string,
onLoginClick: PropTypes.func, onLoginClick: PropTypes.func,
onComplete: PropTypes.func.isRequired, onComplete: PropTypes.func.isRequired,
}, },
getInitialState: function() { getInitialState: function() {
return { return {
enteredHsUrl: this.props.customHsUrl || this.props.defaultHsUrl,
enteredIsUrl: this.props.customIsUrl || this.props.defaultIsUrl,
phase: PHASE_FORGOT, phase: PHASE_FORGOT,
email: "", email: "",
password: "", password: "",
@ -70,11 +56,11 @@ module.exports = React.createClass({
}; };
}, },
submitPasswordReset: function(hsUrl, identityUrl, email, password) { submitPasswordReset: function(email, password) {
this.setState({ this.setState({
phase: PHASE_SENDING_EMAIL, phase: PHASE_SENDING_EMAIL,
}); });
this.reset = new PasswordReset(hsUrl, identityUrl); this.reset = new PasswordReset(this.props.serverConfig.hsUrl, this.props.serverConfig.isUrl);
this.reset.resetPassword(email, password).done(() => { this.reset.resetPassword(email, password).done(() => {
this.setState({ this.setState({
phase: PHASE_EMAIL_SENT, phase: PHASE_EMAIL_SENT,
@ -103,13 +89,6 @@ module.exports = React.createClass({
onSubmitForm: function(ev) { onSubmitForm: function(ev) {
ev.preventDefault(); ev.preventDefault();
// Don't allow the user to register if there's a discovery error
// Without this, the user could end up registering on the wrong homeserver.
if (this.props.defaultServerDiscoveryError) {
this.setState({errorText: this.props.defaultServerDiscoveryError});
return;
}
if (!this.state.email) { if (!this.state.email) {
this.showErrorDialog(_t('The email address linked to your account must be entered.')); this.showErrorDialog(_t('The email address linked to your account must be entered.'));
} else if (!this.state.password || !this.state.password2) { } else if (!this.state.password || !this.state.password2) {
@ -132,10 +111,7 @@ module.exports = React.createClass({
button: _t('Continue'), button: _t('Continue'),
onFinished: (confirmed) => { onFinished: (confirmed) => {
if (confirmed) { if (confirmed) {
this.submitPasswordReset( this.submitPasswordReset(this.state.email, this.state.password);
this.state.enteredHsUrl, this.state.enteredIsUrl,
this.state.email, this.state.password,
);
} }
}, },
}); });
@ -148,19 +124,13 @@ module.exports = React.createClass({
}); });
}, },
onServerConfigChange: function(config) { async onServerDetailsNextPhaseClick(ev) {
const newState = {};
if (config.hsUrl !== undefined) {
newState.enteredHsUrl = config.hsUrl;
}
if (config.isUrl !== undefined) {
newState.enteredIsUrl = config.isUrl;
}
this.setState(newState);
},
onServerDetailsNextPhaseClick(ev) {
ev.stopPropagation(); ev.stopPropagation();
// TODO: TravisR - Capture the user's input somehow else
if (this._serverConfigRef) {
// Just to make sure the user's input gets captured
await this._serverConfigRef.validateServer();
}
this.setState({ this.setState({
phase: PHASE_FORGOT, phase: PHASE_FORGOT,
}); });
@ -196,13 +166,12 @@ module.exports = React.createClass({
return null; return null;
} }
// TODO: TravisR - Pull out server discovery from ServerConfig to disable the next button?
return <div> return <div>
<ServerConfig <ServerConfig
defaultHsUrl={this.props.defaultHsUrl} ref={r => this._serverConfigRef = r}
defaultIsUrl={this.props.defaultIsUrl} serverConfig={this.props.serverConfig}
customHsUrl={this.state.enteredHsUrl} onServerConfigChange={this.props.onServerConfigChange}
customIsUrl={this.state.enteredIsUrl}
onServerConfigChange={this.onServerConfigChange}
delayTimeMs={0} /> delayTimeMs={0} />
<AccessibleButton className="mx_Login_submit" <AccessibleButton className="mx_Login_submit"
onClick={this.onServerDetailsNextPhaseClick} onClick={this.onServerDetailsNextPhaseClick}
@ -221,25 +190,14 @@ module.exports = React.createClass({
errorText = <div className="mx_Login_error">{ err }</div>; errorText = <div className="mx_Login_error">{ err }</div>;
} }
let yourMatrixAccountText = _t('Your Matrix account'); let yourMatrixAccountText = _t('Your Matrix account on %(serverName)s', {
if (this.state.enteredHsUrl === this.props.defaultHsUrl && this.props.defaultServerName) { serverName: this.props.serverConfig.hsName,
yourMatrixAccountText = _t('Your Matrix account on %(serverName)s', {
serverName: this.props.defaultServerName,
}); });
} else { if (this.props.serverConfig.hsNameIsDifferent) {
try { // TODO: TravisR - Use tooltip to underline
const parsedHsUrl = new URL(this.state.enteredHsUrl); yourMatrixAccountText = _t('Your Matrix account on <underlinedServerName />', {}, {
yourMatrixAccountText = _t('Your Matrix account on %(serverName)s', { 'underlinedServerName': () => <u>{this.props.serverConfig.hsName}</u>,
serverName: parsedHsUrl.hostname,
}); });
} catch (e) {
errorText = <div className="mx_Login_error">{_t(
"The homeserver URL %(hsUrl)s doesn't seem to be valid URL. Please " +
"enter a valid URL including the protocol prefix.",
{
hsUrl: this.state.enteredHsUrl,
})}</div>;
}
} }
// If custom URLs are allowed, wire up the server details edit link. // If custom URLs are allowed, wire up the server details edit link.