Files
ThreadNet-Web/src/components/views/login/PasswordLogin.js
T

274 lines
9.8 KiB
JavaScript
Raw Normal View History

/*
2016-01-07 04:06:39 +00:00
Copyright 2015, 2016 OpenMarket Ltd
2017-03-14 11:50:13 +00:00
Copyright 2017 Vector Creations 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.
*/
2016-08-03 15:59:17 +01:00
import React from 'react';
2017-12-26 14:03:18 +13:00
import PropTypes from 'prop-types';
2016-08-03 15:59:17 +01:00
import classNames from 'classnames';
2017-03-14 11:50:13 +00:00
import sdk from '../../../index';
2017-05-25 11:39:08 +01:00
import { _t } from '../../../languageHandler';
2016-08-03 15:59:17 +01:00
import {field_input_incorrect} from '../../../UiEffects';
import SdkConfig from '../../../SdkConfig';
/**
* A pure UI component which displays a username/password form.
*/
class PasswordLogin extends React.Component {
static defaultProps = {
onUsernameChanged: function() {},
onPasswordChanged: function() {},
onPhoneCountryChanged: function() {},
onPhoneNumberChanged: function() {},
initialUsername: "",
initialPhoneCountry: "",
initialPhoneNumber: "",
initialPassword: "",
loginIncorrect: false,
hsDomain: "",
}
constructor(props) {
super(props);
this.state = {
username: this.props.initialUsername,
password: this.props.initialPassword,
2017-03-14 11:50:13 +00:00
phoneCountry: this.props.initialPhoneCountry,
phoneNumber: this.props.initialPhoneNumber,
loginType: PasswordLogin.LOGIN_FIELD_MXID,
};
this.onSubmitForm = this.onSubmitForm.bind(this);
this.onUsernameChanged = this.onUsernameChanged.bind(this);
this.onLoginTypeChange = this.onLoginTypeChange.bind(this);
this.onPhoneCountryChanged = this.onPhoneCountryChanged.bind(this);
this.onPhoneNumberChanged = this.onPhoneNumberChanged.bind(this);
this.onPasswordChanged = this.onPasswordChanged.bind(this);
}
componentWillMount() {
2016-08-03 15:59:17 +01:00
this._passwordField = null;
}
2016-08-03 15:59:17 +01:00
componentWillReceiveProps(nextProps) {
2016-08-03 15:59:17 +01:00
if (!this.props.loginIncorrect && nextProps.loginIncorrect) {
field_input_incorrect(this._passwordField);
}
}
2016-08-03 15:59:17 +01:00
onSubmitForm(ev) {
ev.preventDefault();
if (this.state.loginType === PasswordLogin.LOGIN_FIELD_PHONE) {
this.props.onSubmit(
2017-06-23 14:48:15 +01:00
'', // XXX: Synapse breaks if you send null here:
this.state.phoneCountry,
this.state.phoneNumber,
this.state.password,
);
return;
}
2017-03-14 11:50:13 +00:00
this.props.onSubmit(
this.state.username,
null,
null,
2017-03-14 11:50:13 +00:00
this.state.password,
);
}
onUsernameChanged(ev) {
this.setState({username: ev.target.value});
this.props.onUsernameChanged(ev.target.value);
}
onLoginTypeChange(loginType) {
this.setState({
loginType: loginType,
2017-10-11 17:56:17 +01:00
username: "", // Reset because email and username use the same state
});
}
onPhoneCountryChanged(country) {
this.setState({
phoneCountry: country.iso2,
phonePrefix: country.prefix,
});
this.props.onPhoneCountryChanged(country.iso2);
}
2017-03-14 11:50:13 +00:00
onPhoneNumberChanged(ev) {
2017-03-14 11:50:13 +00:00
this.setState({phoneNumber: ev.target.value});
this.props.onPhoneNumberChanged(ev.target.value);
}
2017-03-14 11:50:13 +00:00
onPasswordChanged(ev) {
this.setState({password: ev.target.value});
this.props.onPasswordChanged(ev.target.value);
}
2017-10-11 14:05:34 +01:00
renderLoginField(loginType, disabled) {
const classes = {
mx_Login_field: true,
mx_Login_field_disabled: disabled,
};
2017-11-16 13:19:36 +00:00
switch (loginType) {
case PasswordLogin.LOGIN_FIELD_EMAIL:
2017-10-11 14:05:34 +01:00
classes.mx_Login_email = true;
return <input
2017-10-11 14:05:34 +01:00
className={classNames(classes)}
key="email_input"
type="text"
name="username" // make it a little easier for browser's remember-password
onChange={this.onUsernameChanged}
placeholder="joe@example.com"
value={this.state.username}
autoFocus
2017-10-11 14:05:34 +01:00
disabled={disabled}
/>;
case PasswordLogin.LOGIN_FIELD_MXID:
2017-10-11 14:05:34 +01:00
classes.mx_Login_username = true;
return <input
2017-10-11 14:05:34 +01:00
className={classNames(classes)}
key="username_input"
type="text"
name="username" // make it a little easier for browser's remember-password
onChange={this.onUsernameChanged}
2017-11-16 13:19:36 +00:00
placeholder={SdkConfig.get().disable_custom_urls ?
_t("Username on %(hs)s", {
2017-11-16 13:19:36 +00:00
hs: this.props.hsUrl.replace(/^https?:\/\//, ''),
}) : _t("User name")}
value={this.state.username}
autoFocus
2017-10-11 14:05:34 +01:00
disabled={disabled}
/>;
case PasswordLogin.LOGIN_FIELD_PHONE:
const CountryDropdown = sdk.getComponent('views.login.CountryDropdown');
2017-10-11 14:05:34 +01:00
classes.mx_Login_phoneNumberField = true;
classes.mx_Login_field_has_prefix = true;
return <div className="mx_Login_phoneSection">
<CountryDropdown
className="mx_Login_phoneCountry mx_Login_field_prefix"
ref="phone_country"
onOptionChange={this.onPhoneCountryChanged}
value={this.state.phoneCountry}
isSmall={true}
showPrefix={true}
2017-10-11 14:05:34 +01:00
disabled={disabled}
/>
<input
2017-10-11 14:05:34 +01:00
className={classNames(classes)}
ref="phoneNumber"
key="phone_input"
type="text"
name="phoneNumber"
onChange={this.onPhoneNumberChanged}
placeholder={_t("Mobile phone number")}
value={this.state.phoneNumber}
autoFocus
2017-10-11 14:05:34 +01:00
disabled={disabled}
/>
</div>;
}
}
render() {
2017-10-11 17:56:17 +01:00
let forgotPasswordJsx;
2016-01-12 17:20:16 +00:00
if (this.props.onForgotPasswordClick) {
forgotPasswordJsx = (
<a className="mx_Login_forgot" onClick={this.props.onForgotPasswordClick} href="#">
{ _t('Forgot your password?') }
2016-01-12 17:20:16 +00:00
</a>
);
}
let matrixIdText = '';
if (this.props.hsUrl) {
try {
const parsedHsUrl = new URL(this.props.hsUrl);
matrixIdText = _t('%(serverName)s Matrix ID', {serverName: parsedHsUrl.hostname});
} catch (e) {
// pass
}
}
2017-10-11 14:05:34 +01:00
const pwFieldClass = classNames({
mx_Login_field: true,
mx_Login_field_disabled: matrixIdText === '',
error: this.props.loginIncorrect,
});
const Dropdown = sdk.getComponent('elements.Dropdown');
const loginField = this.renderLoginField(this.state.loginType, matrixIdText === '');
2017-10-25 01:30:09 +01:00
let loginType;
if (!SdkConfig.get().disable_3pid_login) {
2017-10-25 01:30:09 +01:00
loginType = (
<div className="mx_Login_type_container">
2017-06-02 00:16:17 +01:00
<label className="mx_Login_type_label">{ _t('Sign in with') }</label>
<Dropdown
className="mx_Login_type_dropdown"
value={this.state.loginType}
disabled={matrixIdText === ''}
onOptionChange={this.onLoginTypeChange}>
2017-10-11 17:56:17 +01:00
<span key={PasswordLogin.LOGIN_FIELD_MXID}>{ matrixIdText }</span>
<span key={PasswordLogin.LOGIN_FIELD_EMAIL}>{ _t('Email address') }</span>
<span key={PasswordLogin.LOGIN_FIELD_PHONE}>{ _t('Phone') }</span>
</Dropdown>
2017-03-14 11:50:13 +00:00
</div>
2017-10-25 01:30:09 +01:00
);
}
return (
<div>
<form onSubmit={this.onSubmitForm}>
{ loginType }
2017-10-11 17:56:17 +01:00
{ loginField }
2016-08-03 15:59:17 +01:00
<input className={pwFieldClass} ref={(e) => {this._passwordField = e;}} type="password"
2016-10-14 15:34:44 +01:00
name="password"
value={this.state.password} onChange={this.onPasswordChanged}
2017-10-11 17:56:17 +01:00
placeholder={_t('Password')}
2017-10-11 14:05:34 +01:00
disabled={matrixIdText === ''}
/>
<br />
2017-10-11 17:56:17 +01:00
{ forgotPasswordJsx }
<input className="mx_Login_submit" type="submit" value={_t('Sign in')} disabled={matrixIdText === ''} />
</form>
</div>
);
}
}
PasswordLogin.LOGIN_FIELD_EMAIL = "login_field_email";
PasswordLogin.LOGIN_FIELD_MXID = "login_field_mxid";
PasswordLogin.LOGIN_FIELD_PHONE = "login_field_phone";
PasswordLogin.propTypes = {
2017-12-26 14:03:18 +13:00
onSubmit: PropTypes.func.isRequired, // fn(username, password)
onForgotPasswordClick: PropTypes.func, // fn()
initialUsername: PropTypes.string,
initialPhoneCountry: PropTypes.string,
initialPhoneNumber: PropTypes.string,
initialPassword: PropTypes.string,
onUsernameChanged: PropTypes.func,
onPhoneCountryChanged: PropTypes.func,
onPhoneNumberChanged: PropTypes.func,
onPasswordChanged: PropTypes.func,
loginIncorrect: PropTypes.bool,
};
module.exports = PasswordLogin;