2016-01-12 20:20:16 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-02-06 18:10:16 +03:00
|
|
|
Copyright 2017, 2018, 2019 New Vector Ltd
|
2016-01-12 20:20:16 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-27 03:23:50 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-10-27 03:23:50 +03:00
|
|
|
import sdk from '../../../index';
|
|
|
|
import Modal from "../../../Modal";
|
2018-04-06 12:27:03 +03:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2016-01-12 20:20:16 +03:00
|
|
|
|
2017-10-27 03:23:50 +03:00
|
|
|
import PasswordReset from "../../../PasswordReset";
|
2016-01-12 20:20:16 +03:00
|
|
|
|
2019-02-06 19:30:53 +03:00
|
|
|
// Phases
|
|
|
|
// Show controls to configure server details
|
|
|
|
const PHASE_SERVER_DETAILS = 0;
|
|
|
|
// Show the forgot password inputs
|
|
|
|
const PHASE_FORGOT = 1;
|
|
|
|
// Email is in the process of being sent
|
|
|
|
const PHASE_SENDING_EMAIL = 2;
|
|
|
|
// Email has been sent
|
|
|
|
const PHASE_EMAIL_SENT = 3;
|
|
|
|
// User has clicked the link in email and completed reset
|
|
|
|
const PHASE_DONE = 4;
|
|
|
|
|
2016-01-12 20:20:16 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ForgotPassword',
|
|
|
|
|
|
|
|
propTypes: {
|
2019-02-20 16:57:21 +03:00
|
|
|
// The default server name to use when the user hasn't specified
|
|
|
|
// one. If set, `defaultHsUrl` and `defaultHsUrl` were derived for this
|
|
|
|
// 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,
|
|
|
|
|
2017-12-26 04:03:18 +03:00
|
|
|
defaultHsUrl: PropTypes.string,
|
|
|
|
defaultIsUrl: PropTypes.string,
|
|
|
|
customHsUrl: PropTypes.string,
|
|
|
|
customIsUrl: PropTypes.string,
|
2019-02-20 16:57:21 +03:00
|
|
|
|
2017-12-26 04:03:18 +03:00
|
|
|
onLoginClick: PropTypes.func,
|
|
|
|
onComplete: PropTypes.func.isRequired,
|
2016-01-12 20:20:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2019-02-06 18:58:31 +03:00
|
|
|
enteredHsUrl: this.props.customHsUrl || this.props.defaultHsUrl,
|
|
|
|
enteredIsUrl: this.props.customIsUrl || this.props.defaultIsUrl,
|
2019-02-06 19:46:49 +03:00
|
|
|
phase: PHASE_FORGOT,
|
2019-02-06 21:26:44 +03:00
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
password2: "",
|
2018-12-05 09:34:57 +03:00
|
|
|
errorText: null,
|
2016-01-12 20:20:16 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
submitPasswordReset: function(hsUrl, identityUrl, email, password) {
|
|
|
|
this.setState({
|
2019-02-06 19:30:53 +03:00
|
|
|
phase: PHASE_SENDING_EMAIL,
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|
|
|
|
this.reset = new PasswordReset(hsUrl, identityUrl);
|
|
|
|
this.reset.resetPassword(email, password).done(() => {
|
|
|
|
this.setState({
|
2019-02-06 19:30:53 +03:00
|
|
|
phase: PHASE_EMAIL_SENT,
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|
|
|
|
}, (err) => {
|
2017-05-23 17:16:31 +03:00
|
|
|
this.showErrorDialog(_t('Failed to send email') + ": " + err.message);
|
2016-01-12 20:20:16 +03:00
|
|
|
this.setState({
|
2019-02-06 21:32:28 +03:00
|
|
|
phase: PHASE_FORGOT,
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|
2017-01-20 17:22:27 +03:00
|
|
|
});
|
2016-01-12 20:20:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onVerify: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
if (!this.reset) {
|
|
|
|
console.error("onVerify called before submitPasswordReset!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.reset.checkEmailLinkClicked().done((res) => {
|
2019-02-06 19:30:53 +03:00
|
|
|
this.setState({ phase: PHASE_DONE });
|
2016-01-12 20:20:16 +03:00
|
|
|
}, (err) => {
|
|
|
|
this.showErrorDialog(err.message);
|
2017-01-20 17:22:27 +03:00
|
|
|
});
|
2016-01-12 20:20:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onSubmitForm: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
2018-12-05 09:34:57 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-01-12 20:20:16 +03:00
|
|
|
if (!this.state.email) {
|
2017-05-23 17:16:31 +03:00
|
|
|
this.showErrorDialog(_t('The email address linked to your account must be entered.'));
|
2017-10-11 19:56:17 +03:00
|
|
|
} else if (!this.state.password || !this.state.password2) {
|
2017-05-27 20:20:35 +03:00
|
|
|
this.showErrorDialog(_t('A new password must be entered.'));
|
2017-10-11 19:56:17 +03:00
|
|
|
} else if (this.state.password !== this.state.password2) {
|
2017-05-23 17:16:31 +03:00
|
|
|
this.showErrorDialog(_t('New passwords must match each other.'));
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
2017-07-27 19:19:18 +03:00
|
|
|
Modal.createTrackedDialog('Forgot Password Warning', '', QuestionDialog, {
|
2017-05-25 20:20:48 +03:00
|
|
|
title: _t('Warning!'),
|
2017-01-25 00:36:55 +03:00
|
|
|
description:
|
|
|
|
<div>
|
2017-05-23 17:16:31 +03:00
|
|
|
{ _t(
|
2019-03-14 00:24:05 +03:00
|
|
|
"Changing your password will reset any end-to-end encryption keys " +
|
|
|
|
"on all of your devices, making encrypted chat history unreadable. Set up " +
|
|
|
|
"Key Backup or export your room keys from another device before resetting your " +
|
|
|
|
"password.",
|
2017-05-27 20:20:35 +03:00
|
|
|
) }
|
2017-01-25 00:36:55 +03:00
|
|
|
</div>,
|
2017-05-23 17:16:31 +03:00
|
|
|
button: _t('Continue'),
|
2017-01-25 00:36:55 +03:00
|
|
|
onFinished: (confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
this.submitPasswordReset(
|
2019-02-06 18:58:31 +03:00
|
|
|
this.state.enteredHsUrl, this.state.enteredIsUrl,
|
2017-10-11 19:56:17 +03:00
|
|
|
this.state.email, this.state.password,
|
2017-01-25 00:36:55 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2016-01-12 20:20:16 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onInputChanged: function(stateKey, ev) {
|
|
|
|
this.setState({
|
2017-10-11 19:56:17 +03:00
|
|
|
[stateKey]: ev.target.value,
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-08-29 16:07:43 +03:00
|
|
|
onServerConfigChange: function(config) {
|
|
|
|
const newState = {};
|
|
|
|
if (config.hsUrl !== undefined) {
|
2019-02-06 18:58:31 +03:00
|
|
|
newState.enteredHsUrl = config.hsUrl;
|
2017-08-29 16:07:43 +03:00
|
|
|
}
|
|
|
|
if (config.isUrl !== undefined) {
|
2019-02-06 18:58:31 +03:00
|
|
|
newState.enteredIsUrl = config.isUrl;
|
2017-08-29 16:07:43 +03:00
|
|
|
}
|
|
|
|
this.setState(newState);
|
2016-01-12 20:20:16 +03:00
|
|
|
},
|
|
|
|
|
2019-02-06 21:30:07 +03:00
|
|
|
onServerDetailsNextPhaseClick(ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.setState({
|
|
|
|
phase: PHASE_FORGOT,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onEditServerDetailsClick(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.setState({
|
|
|
|
phase: PHASE_SERVER_DETAILS,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
Fix browser navigation not working between /home, /login, /register, etc
All of the anchors were pointed at `#` which, when clicked, would trigger a hash change in the browser. This change races the change made by the screen handling where the screen handling ends up losing. Because the hash is then tracked as empty rather than `#/login` (for example), the state machine considers future changes as no-ops and doesn't do anything with them.
By using `preventDefault` and `stopPropagation` on the anchor click events, we prevent the browser from automatically going to an empty hash, which then means the screen handling isn't racing the browser, and the hash change state machine doesn't no-op.
After applying that fix, going between pages worked great unless you were going from /login to /home. This is because the MatrixChat state machine was now out of sync (a `view` of `LOGIN` but a `page` of `HomePage` - an invalid state). All we have to do here is ensure the right view is used when navigating to the homepage.
Fixes https://github.com/vector-im/riot-web/issues/4061
Note: the concerns in 4061 about logging out upon entering the view appear to have been solved. Navigating to the login page doesn't obliterate your session, at least in my testing.
2018-12-21 03:26:13 +03:00
|
|
|
onLoginClick: function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
this.props.onLoginClick();
|
|
|
|
},
|
|
|
|
|
2016-01-12 20:20:16 +03:00
|
|
|
showErrorDialog: function(body, title) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2017-08-10 17:21:01 +03:00
|
|
|
Modal.createTrackedDialog('Forgot Password Error', '', ErrorDialog, {
|
2016-01-12 20:20:16 +03:00
|
|
|
title: title,
|
2017-05-23 17:16:31 +03:00
|
|
|
description: body,
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-02-06 19:46:49 +03:00
|
|
|
renderServerDetails() {
|
|
|
|
const ServerConfig = sdk.getComponent("auth.ServerConfig");
|
2019-02-06 21:30:07 +03:00
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
2019-02-06 19:46:49 +03:00
|
|
|
|
2019-02-06 21:30:07 +03:00
|
|
|
if (SdkConfig.get()['disable_custom_urls']) {
|
|
|
|
return null;
|
2019-02-06 19:46:49 +03:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:30:07 +03:00
|
|
|
return <div>
|
2019-02-07 12:44:28 +03:00
|
|
|
<ServerConfig
|
2019-02-06 21:30:07 +03:00
|
|
|
defaultHsUrl={this.props.defaultHsUrl}
|
|
|
|
defaultIsUrl={this.props.defaultIsUrl}
|
|
|
|
customHsUrl={this.state.enteredHsUrl}
|
|
|
|
customIsUrl={this.state.enteredIsUrl}
|
|
|
|
onServerConfigChange={this.onServerConfigChange}
|
|
|
|
delayTimeMs={0} />
|
|
|
|
<AccessibleButton className="mx_Login_submit"
|
|
|
|
onClick={this.onServerDetailsNextPhaseClick}
|
|
|
|
>
|
|
|
|
{_t("Next")}
|
|
|
|
</AccessibleButton>
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderForgot() {
|
2019-03-05 18:39:51 +03:00
|
|
|
const Field = sdk.getComponent('elements.Field');
|
|
|
|
|
2019-02-06 19:46:49 +03:00
|
|
|
let errorText = null;
|
|
|
|
const err = this.state.errorText || this.props.defaultServerDiscoveryError;
|
|
|
|
if (err) {
|
|
|
|
errorText = <div className="mx_Login_error">{ err }</div>;
|
|
|
|
}
|
|
|
|
|
2019-02-20 17:29:57 +03:00
|
|
|
let yourMatrixAccountText = _t('Your Matrix account');
|
2019-04-17 11:35:45 +03:00
|
|
|
if (this.state.enteredHsUrl === this.props.defaultHsUrl && this.props.defaultServerName) {
|
2019-02-20 17:29:57 +03:00
|
|
|
yourMatrixAccountText = _t('Your Matrix account on %(serverName)s', {
|
2019-02-20 16:57:21 +03:00
|
|
|
serverName: this.props.defaultServerName,
|
2019-02-06 19:46:49 +03:00
|
|
|
});
|
2019-02-20 16:57:21 +03:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const parsedHsUrl = new URL(this.state.enteredHsUrl);
|
2019-02-20 17:29:57 +03:00
|
|
|
yourMatrixAccountText = _t('Your Matrix account on %(serverName)s', {
|
2019-02-20 16:57:21 +03:00
|
|
|
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>;
|
|
|
|
}
|
2019-02-06 19:46:49 +03:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:30:07 +03:00
|
|
|
// If custom URLs are allowed, wire up the server details edit link.
|
|
|
|
let editLink = null;
|
|
|
|
if (!SdkConfig.get()['disable_custom_urls']) {
|
|
|
|
editLink = <a className="mx_AuthBody_editServerDetails"
|
|
|
|
href="#" onClick={this.onEditServerDetailsClick}
|
|
|
|
>
|
|
|
|
{_t('Change')}
|
|
|
|
</a>;
|
|
|
|
}
|
|
|
|
|
2019-02-06 19:46:49 +03:00
|
|
|
return <div>
|
|
|
|
<h3>
|
|
|
|
{yourMatrixAccountText}
|
2019-02-06 21:30:07 +03:00
|
|
|
{editLink}
|
2019-02-06 19:46:49 +03:00
|
|
|
</h3>
|
2019-02-06 21:10:06 +03:00
|
|
|
{errorText}
|
2019-02-06 19:46:49 +03:00
|
|
|
<form onSubmit={this.onSubmitForm}>
|
|
|
|
<div className="mx_AuthBody_fieldRow">
|
2019-03-05 18:39:51 +03:00
|
|
|
<Field
|
|
|
|
id="mx_ForgotPassword_email"
|
2019-02-06 19:46:49 +03:00
|
|
|
name="reset_email" // define a name so browser's password autofill gets less confused
|
2019-03-05 18:39:51 +03:00
|
|
|
type="text"
|
|
|
|
label={_t('Email')}
|
2019-02-06 19:46:49 +03:00
|
|
|
value={this.state.email}
|
|
|
|
onChange={this.onInputChanged.bind(this, "email")}
|
2019-03-05 18:39:51 +03:00
|
|
|
autoFocus
|
|
|
|
/>
|
2019-02-06 19:46:49 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_AuthBody_fieldRow">
|
2019-03-05 18:39:51 +03:00
|
|
|
<Field
|
|
|
|
id="mx_ForgotPassword_password"
|
2019-02-06 19:46:49 +03:00
|
|
|
name="reset_password"
|
2019-03-05 18:39:51 +03:00
|
|
|
type="password"
|
|
|
|
label={_t('Password')}
|
2019-02-06 19:46:49 +03:00
|
|
|
value={this.state.password}
|
|
|
|
onChange={this.onInputChanged.bind(this, "password")}
|
2019-03-05 18:39:51 +03:00
|
|
|
/>
|
|
|
|
<Field
|
|
|
|
id="mx_ForgotPassword_passwordConfirm"
|
2019-02-06 19:46:49 +03:00
|
|
|
name="reset_password_confirm"
|
2019-03-05 18:39:51 +03:00
|
|
|
type="password"
|
|
|
|
label={_t('Confirm')}
|
2019-02-06 19:46:49 +03:00
|
|
|
value={this.state.password2}
|
|
|
|
onChange={this.onInputChanged.bind(this, "password2")}
|
2019-03-05 18:39:51 +03:00
|
|
|
/>
|
2019-02-06 19:46:49 +03:00
|
|
|
</div>
|
|
|
|
<span>{_t(
|
|
|
|
'A verification email will be sent to your inbox to confirm ' +
|
|
|
|
'setting your new password.',
|
|
|
|
)}</span>
|
|
|
|
<input className="mx_Login_submit" type="submit" value={_t('Send Reset Email')} />
|
|
|
|
</form>
|
|
|
|
<a className="mx_AuthBody_changeFlow" onClick={this.onLoginClick} href="#">
|
|
|
|
{_t('Sign in instead')}
|
|
|
|
</a>
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderSendingEmail() {
|
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
return <Spinner />;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderEmailSent() {
|
|
|
|
return <div>
|
|
|
|
{_t("An email has been sent to %(emailAddress)s. Once you've followed the " +
|
|
|
|
"link it contains, click below.", { emailAddress: this.state.email })}
|
|
|
|
<br />
|
|
|
|
<input className="mx_Login_submit" type="button" onClick={this.onVerify}
|
|
|
|
value={_t('I have verified my email address')} />
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
|
|
|
|
renderDone() {
|
|
|
|
return <div>
|
2019-02-07 12:57:31 +03:00
|
|
|
<p>{_t("Your password has been reset.")}</p>
|
|
|
|
<p>{_t(
|
|
|
|
"You have been logged out of all devices and will no longer receive " +
|
|
|
|
"push notifications. To re-enable notifications, sign in again on each " +
|
|
|
|
"device.",
|
|
|
|
)}</p>
|
2019-02-06 19:46:49 +03:00
|
|
|
<input className="mx_Login_submit" type="button" onClick={this.props.onComplete}
|
|
|
|
value={_t('Return to login screen')} />
|
|
|
|
</div>;
|
|
|
|
},
|
|
|
|
|
2016-01-12 20:20:16 +03:00
|
|
|
render: function() {
|
2019-01-22 03:33:17 +03:00
|
|
|
const AuthPage = sdk.getComponent("auth.AuthPage");
|
|
|
|
const AuthHeader = sdk.getComponent("auth.AuthHeader");
|
2019-01-23 04:28:23 +03:00
|
|
|
const AuthBody = sdk.getComponent("auth.AuthBody");
|
2016-01-12 20:20:16 +03:00
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let resetPasswordJsx;
|
2019-02-06 19:46:49 +03:00
|
|
|
switch (this.state.phase) {
|
|
|
|
case PHASE_SERVER_DETAILS:
|
|
|
|
resetPasswordJsx = this.renderServerDetails();
|
|
|
|
break;
|
|
|
|
case PHASE_FORGOT:
|
|
|
|
resetPasswordJsx = this.renderForgot();
|
|
|
|
break;
|
|
|
|
case PHASE_SENDING_EMAIL:
|
|
|
|
resetPasswordJsx = this.renderSendingEmail();
|
|
|
|
break;
|
|
|
|
case PHASE_EMAIL_SENT:
|
|
|
|
resetPasswordJsx = this.renderEmailSent();
|
|
|
|
break;
|
|
|
|
case PHASE_DONE:
|
|
|
|
resetPasswordJsx = this.renderDone();
|
|
|
|
break;
|
2016-01-12 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-01-22 03:33:17 +03:00
|
|
|
<AuthPage>
|
2019-01-22 03:49:28 +03:00
|
|
|
<AuthHeader />
|
2019-01-23 04:28:23 +03:00
|
|
|
<AuthBody>
|
2019-01-24 00:03:43 +03:00
|
|
|
<h2> { _t('Set a new password') } </h2>
|
2019-01-23 04:28:23 +03:00
|
|
|
{resetPasswordJsx}
|
|
|
|
</AuthBody>
|
2019-01-22 03:33:17 +03:00
|
|
|
</AuthPage>
|
2016-01-12 20:20:16 +03:00
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-01-12 20:20:16 +03:00
|
|
|
});
|