/* Copyright 2015, 2016 OpenMarket Ltd 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. */ 'use strict'; var React = require('react'); var MatrixClientPeg = require("../../../MatrixClientPeg"); var Modal = require("../../../Modal"); var sdk = require("../../../index"); import AccessibleButton from '../elements/AccessibleButton'; import sessionStore from '../../../stores/SessionStore'; module.exports = React.createClass({ displayName: 'ChangePassword', propTypes: { onFinished: React.PropTypes.func, onError: React.PropTypes.func, onCheckPassword: React.PropTypes.func, rowClassName: React.PropTypes.string, rowLabelClassName: React.PropTypes.string, rowInputClassName: React.PropTypes.string, buttonClassName: React.PropTypes.string, }, Phases: { Edit: "edit", Uploading: "uploading", Error: "error" }, getDefaultProps: function() { return { onFinished: function() {}, onError: function() {}, onCheckPassword: function(oldPass, newPass, confirmPass) { if (newPass !== confirmPass) { return { error: "New passwords don't match." }; } else if (!newPass || newPass.length === 0) { return { error: "Passwords can't be empty" }; } } }; }, getInitialState: function() { return { phase: this.Phases.Edit, cachedPassword: null, }; }, componentWillMount: function() { this.sessionStore = sessionStore; this.sessionStore.on('update', this.setStateFromSessionStore); this.setStateFromSessionStore(); }, setStateFromSessionStore: function() { this.setState({ cachedPassword: this.sessionStore.getCachedPassword(), }); }, changePassword: function(old_password, new_password) { var cli = MatrixClientPeg.get(); var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog"); Modal.createDialog(QuestionDialog, { title: "Warning", description:
Changing password will currently reset any end-to-end encryption keys on all devices, making encrypted chat history unreadable, unless you first export your room keys and re-import them afterwards. In future this will be improved.
, button: "Continue", extraButtons: [ ], onFinished: (confirmed) => { if (confirmed) { var authDict = { type: 'm.login.password', user: cli.credentials.userId, password: old_password }; this.setState({ phase: this.Phases.Uploading }); var self = this; cli.setPassword(authDict, new_password).then(function() { self.props.onFinished(); }, function(err) { self.props.onError(err); }).finally(function() { self.setState({ phase: self.Phases.Edit }); }).done(); } }, }); }, _onExportE2eKeysClicked: function() { Modal.createDialogAsync( (cb) => { require.ensure(['../../../async-components/views/dialogs/ExportE2eKeysDialog'], () => { cb(require('../../../async-components/views/dialogs/ExportE2eKeysDialog')); }, "e2e-export"); }, { matrixClient: MatrixClientPeg.get(), } ); }, onClickChange: function() { var old_password = this.state.cachedPassword || this.refs.old_input.value; var new_password = this.refs.new_input.value; var confirm_password = this.refs.confirm_input.value; var err = this.props.onCheckPassword( old_password, new_password, confirm_password ); if (err) { this.props.onError(err); } else { this.changePassword(old_password, new_password); } }, render: function() { const rowClassName = this.props.rowClassName; const rowLabelClassName = this.props.rowLabelClassName; const rowInputClassName = this.props.rowInputClassName; const buttonClassName = this.props.buttonClassName; let currentPassword = null; if (!this.state.cachedPassword) { currentPassword =
; } switch (this.state.phase) { case this.Phases.Edit: return (
{ currentPassword }
Change Password
); case this.Phases.Uploading: var Loader = sdk.getComponent("elements.Spinner"); return (
); } } });