2015-11-26 20:10:36 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015 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");
|
2015-12-23 18:38:28 +03:00
|
|
|
var sdk = require("../../../index");
|
2015-11-26 20:10:36 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ChangePassword',
|
|
|
|
propTypes: {
|
|
|
|
onFinished: React.PropTypes.func,
|
2015-12-23 18:38:28 +03:00
|
|
|
onError: React.PropTypes.func,
|
|
|
|
onCheckPassword: React.PropTypes.func,
|
|
|
|
rowClassName: React.PropTypes.string,
|
|
|
|
rowLabelClassName: React.PropTypes.string,
|
|
|
|
rowInputClassName: React.PropTypes.string,
|
|
|
|
buttonClassName: React.PropTypes.string
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
Phases: {
|
|
|
|
Edit: "edit",
|
|
|
|
Uploading: "uploading",
|
2015-12-23 18:38:28 +03:00
|
|
|
Error: "error"
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
onFinished: function() {},
|
2015-12-23 18:38:28 +03:00
|
|
|
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"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 20:10:36 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
phase: this.Phases.Edit,
|
|
|
|
errorString: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
changePassword: function(old_password, new_password) {
|
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
|
|
|
|
var authDict = {
|
|
|
|
type: 'm.login.password',
|
|
|
|
user: cli.credentials.userId,
|
|
|
|
password: old_password
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
phase: this.Phases.Uploading,
|
|
|
|
errorString: '',
|
2015-12-23 18:38:28 +03:00
|
|
|
});
|
2015-11-26 20:10:36 +03:00
|
|
|
|
|
|
|
var self = this;
|
2015-12-23 18:38:28 +03:00
|
|
|
cli.setPassword(authDict, new_password).then(function() {
|
|
|
|
self.props.onFinished();
|
2015-11-26 20:10:36 +03:00
|
|
|
}, function(err) {
|
2015-12-23 18:38:28 +03:00
|
|
|
self.props.onError(err);
|
|
|
|
}).finally(function() {
|
2015-11-26 20:10:36 +03:00
|
|
|
self.setState({
|
2015-12-23 18:38:28 +03:00
|
|
|
phase: self.Phases.Edit,
|
|
|
|
errorString: ""
|
|
|
|
});
|
|
|
|
}).done();
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onClickChange: function() {
|
|
|
|
var old_password = this.refs.old_input.value;
|
|
|
|
var new_password = this.refs.new_input.value;
|
|
|
|
var confirm_password = this.refs.confirm_input.value;
|
2015-12-23 18:38:28 +03:00
|
|
|
var err = this.props.onCheckPassword(
|
|
|
|
old_password, new_password, confirm_password
|
|
|
|
);
|
|
|
|
if (err) {
|
|
|
|
this.props.onError(err);
|
|
|
|
}
|
|
|
|
else {
|
2015-11-26 20:10:36 +03:00
|
|
|
this.changePassword(old_password, new_password);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2015-12-23 18:38:28 +03:00
|
|
|
var rowClassName = this.props.rowClassName;
|
|
|
|
var rowLabelClassName = this.props.rowLabelClassName;
|
|
|
|
var rowInputClassName = this.props.rowInputClassName
|
|
|
|
var buttonClassName = this.props.buttonClassName;
|
|
|
|
|
2015-11-26 20:10:36 +03:00
|
|
|
switch (this.state.phase) {
|
|
|
|
case this.Phases.Edit:
|
|
|
|
return (
|
2015-12-23 18:38:28 +03:00
|
|
|
<div className={this.props.className}>
|
|
|
|
<div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="passwordold">Old password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="passwordold" type="password" ref="old_input" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="password1">New password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="password1" type="password" ref="new_input" />
|
|
|
|
</div>
|
2015-11-26 20:10:36 +03:00
|
|
|
</div>
|
2015-12-23 18:38:28 +03:00
|
|
|
<div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="password2">Confirm password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="password2" type="password" ref="confirm_input" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={buttonClassName} onClick={this.onClickChange}>
|
|
|
|
Change Password
|
2015-11-26 20:10:36 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
case this.Phases.Uploading:
|
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
return (
|
|
|
|
<div className="mx_Dialog_content">
|
|
|
|
<Loader />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|