/* Copyright 2015, 2016 OpenMarket Ltd 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. */ import React from 'react'; import classNames from 'classnames'; import sdk from '../../../index'; import { _t } from '../../../languageHandler'; import {field_input_incorrect} from '../../../UiEffects'; import UserSettingsStore from '../../../UserSettingsStore'; /** * 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, 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() { this._passwordField = null; } componentWillReceiveProps(nextProps) { if (!this.props.loginIncorrect && nextProps.loginIncorrect) { field_input_incorrect(this._passwordField); } } onSubmitForm(ev) { ev.preventDefault(); if (this.state.loginType === PasswordLogin.LOGIN_FIELD_PHONE) { this.props.onSubmit( '', // XXX: Synapse breaks if you send null here: this.state.phoneCountry, this.state.phoneNumber, this.state.password, ); return; } this.props.onSubmit( this.state.username, null, null, this.state.password, ); } onUsernameChanged(ev) { this.setState({username: ev.target.value}); this.props.onUsernameChanged(ev.target.value); } onLoginTypeChange(loginType) { this.setState({ loginType: loginType, 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); } onPhoneNumberChanged(ev) { this.setState({phoneNumber: ev.target.value}); this.props.onPhoneNumberChanged(ev.target.value); } onPasswordChanged(ev) { this.setState({password: ev.target.value}); this.props.onPasswordChanged(ev.target.value); } renderLoginField(loginType, disabled) { const classes = { mx_Login_field: true, mx_Login_field_disabled: disabled, }; switch(loginType) { case PasswordLogin.LOGIN_FIELD_EMAIL: classes.mx_Login_email = true; return ; case PasswordLogin.LOGIN_FIELD_MXID: classes.mx_Login_username = true; return ; case PasswordLogin.LOGIN_FIELD_PHONE: const CountryDropdown = sdk.getComponent('views.login.CountryDropdown'); classes.mx_Login_phoneNumberField = true; classes.mx_Login_field_has_prefix = true; return
; } } render() { let forgotPasswordJsx; if (this.props.onForgotPasswordClick) { forgotPasswordJsx = ( { _t('Forgot your password?') } ); } 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 } } 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 === ''); const theme = UserSettingsStore.getTheme(); let loginType; if (theme !== 'status') { loginType = (
{ matrixIdText } { _t('Email address') } { _t('Phone') }
); } return (
{ loginType } { loginField } {this._passwordField = e;}} type="password" name="password" value={this.state.password} onChange={this.onPasswordChanged} placeholder={_t('Password')} disabled={matrixIdText === ''} />
{ forgotPasswordJsx }
); } } PasswordLogin.LOGIN_FIELD_EMAIL = "login_field_email"; PasswordLogin.LOGIN_FIELD_MXID = "login_field_mxid"; PasswordLogin.LOGIN_FIELD_PHONE = "login_field_phone"; PasswordLogin.propTypes = { onSubmit: React.PropTypes.func.isRequired, // fn(username, password) onForgotPasswordClick: React.PropTypes.func, // fn() initialUsername: React.PropTypes.string, initialPhoneCountry: React.PropTypes.string, initialPhoneNumber: React.PropTypes.string, initialPassword: React.PropTypes.string, onUsernameChanged: React.PropTypes.func, onPhoneCountryChanged: React.PropTypes.func, onPhoneNumberChanged: React.PropTypes.func, onPasswordChanged: React.PropTypes.func, loginIncorrect: React.PropTypes.bool, }; module.exports = PasswordLogin;