您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LoginDialog.native.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { Text, TextInput, View } from 'react-native';
  4. import { connect as reduxConnect } from 'react-redux';
  5. import { connect, toJid } from '../../base/connection';
  6. import { Dialog } from '../../base/dialog';
  7. import { translate } from '../../base/i18n';
  8. import { JitsiConnectionErrors } from '../../base/lib-jitsi-meet';
  9. import { authenticateAndUpgradeRole, cancelLogin } from '../actions';
  10. import styles from './styles';
  11. /**
  12. * Dialog asks user for username and password.
  13. *
  14. * First authentication configuration that it will deal with is the main XMPP
  15. * domain (config.hosts.domain) with password authentication. A LoginDialog
  16. * will be opened after 'CONNECTION_FAILED' action with
  17. * 'JitsiConnectionErrors.PASSWORD_REQUIRED' error. After username and password
  18. * are entered a new 'connect' action from 'features/base/connection' will be
  19. * triggered which will result in new XMPP connection. The conference will start
  20. * if the credentials are correct.
  21. *
  22. * The second setup is the main XMPP domain with password plus guest domain with
  23. * anonymous access configured under 'config.hosts.anonymousdomain'. In such
  24. * case user connects from the anonymous domain, but if the room does not exist
  25. * yet, Jicofo will not allow to start new conference. This will trigger
  26. * 'CONFERENCE_FAILED' action with JitsiConferenceErrors.AUTHENTICATION_REQUIRED
  27. * error and 'authRequired' value of 'features/base/conference' will hold
  28. * the {@link JitsiConference} instance. If user decides to authenticate, a
  29. * new/separate XMPP connection is established and authentication is performed.
  30. * In case it succeeds, Jicofo will assign new session ID which then can be used
  31. * from the anonymous domain connection to create and join the room. This part
  32. * is done by {@link JitsiConference#authenticateAndUpgradeRole} in
  33. * lib-jitsi-meet.
  34. *
  35. * See {@link https://github.com/jitsi/jicofo#secure-domain} for a description
  36. * of the configuration parameters.
  37. */
  38. class LoginDialog extends Component {
  39. /**
  40. * LoginDialog component's property types.
  41. *
  42. * @static
  43. */
  44. static propTypes = {
  45. /**
  46. * {@link JitsiConference} that needs authentication - will hold a valid
  47. * value in XMPP login + guest access mode.
  48. */
  49. _conference: PropTypes.object,
  50. /**
  51. *
  52. */
  53. _configHosts: PropTypes.object,
  54. /**
  55. * Indicates if the dialog should display "connecting" status message.
  56. */
  57. _connecting: PropTypes.bool,
  58. /**
  59. * The error which occurred during login/authentication.
  60. */
  61. _error: PropTypes.string,
  62. /**
  63. * Any extra details about the error provided by lib-jitsi-meet.
  64. */
  65. _errorDetails: PropTypes.string,
  66. /**
  67. * Redux store dispatch method.
  68. */
  69. dispatch: PropTypes.func,
  70. /**
  71. * Invoked to obtain translated strings.
  72. */
  73. t: PropTypes.func
  74. };
  75. /**
  76. * Initializes a new LoginDialog instance.
  77. *
  78. * @param {Object} props - The read-only properties with which the new
  79. * instance is to be initialized.
  80. */
  81. constructor(props) {
  82. super(props);
  83. this.state = {
  84. username: '',
  85. password: ''
  86. };
  87. // Bind event handlers so they are only bound once per instance.
  88. this._onCancel = this._onCancel.bind(this);
  89. this._onLogin = this._onLogin.bind(this);
  90. this._onPasswordChange = this._onPasswordChange.bind(this);
  91. this._onUsernameChange = this._onUsernameChange.bind(this);
  92. }
  93. /**
  94. * Implements React's {@link Component#render()}.
  95. *
  96. * @inheritdoc
  97. * @returns {ReactElement}
  98. */
  99. render() {
  100. const {
  101. _connecting: connecting,
  102. _error: error,
  103. _errorDetails: errorDetails,
  104. t
  105. } = this.props;
  106. let messageKey = '';
  107. const messageOptions = {};
  108. if (error === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  109. messageKey = 'dialog.incorrectPassword';
  110. } else if (error) {
  111. messageKey = 'dialog.connectErrorWithMsg';
  112. messageOptions.msg = `${error} ${errorDetails}`;
  113. }
  114. return (
  115. <Dialog
  116. okDisabled = { connecting }
  117. onCancel = { this._onCancel }
  118. onSubmit = { this._onLogin }
  119. titleKey = 'dialog.passwordRequired'>
  120. <View style = { styles.loginDialog }>
  121. <TextInput
  122. onChangeText = { this._onUsernameChange }
  123. placeholder = { 'user@domain.com' }
  124. style = { styles.loginDialogTextInput }
  125. value = { this.state.username } />
  126. <TextInput
  127. onChangeText = { this._onPasswordChange }
  128. placeholder = { t('dialog.userPassword') }
  129. secureTextEntry = { true }
  130. style = { styles.loginDialogTextInput }
  131. value = { this.state.password } />
  132. <Text style = { styles.loginDialogText }>
  133. {
  134. error
  135. ? t(messageKey, messageOptions)
  136. : connecting
  137. ? t('connection.CONNECTING')
  138. : ''
  139. }
  140. </Text>
  141. </View>
  142. </Dialog>
  143. );
  144. }
  145. /**
  146. * Called when user edits the username.
  147. *
  148. * @param {string} text - A new username value entered by user.
  149. * @returns {void}
  150. * @private
  151. */
  152. _onUsernameChange(text) {
  153. this.setState({
  154. username: text
  155. });
  156. }
  157. /**
  158. * Called when user edits the password.
  159. *
  160. * @param {string} text - A new password value entered by user.
  161. * @returns {void}
  162. * @private
  163. */
  164. _onPasswordChange(text) {
  165. this.setState({
  166. password: text
  167. });
  168. }
  169. /**
  170. * Notifies this LoginDialog that it has been dismissed by cancel.
  171. *
  172. * @private
  173. * @returns {void}
  174. */
  175. _onCancel() {
  176. this.props.dispatch(cancelLogin());
  177. }
  178. /**
  179. * Notifies this LoginDialog that the login button (OK) has been pressed by
  180. * the user.
  181. *
  182. * @private
  183. * @returns {void}
  184. */
  185. _onLogin() {
  186. const { _conference: conference } = this.props;
  187. const { username, password } = this.state;
  188. const jid = toJid(username, this.props._configHosts);
  189. // If there's a conference it means that the connection has succeeded,
  190. // but authentication is required in order to join the room.
  191. if (conference) {
  192. this.props.dispatch(
  193. authenticateAndUpgradeRole(jid, password, conference));
  194. } else {
  195. this.props.dispatch(connect(jid, password));
  196. }
  197. }
  198. }
  199. /**
  200. * Maps (parts of) the Redux state to the associated props for the
  201. * {@code LoginDialog} component.
  202. *
  203. * @param {Object} state - The Redux state.
  204. * @private
  205. * @returns {{
  206. * _conference: JitsiConference,
  207. * _configHosts: Object,
  208. * _connecting: boolean,
  209. * _error: string,
  210. * _errorDetails: string
  211. * }}
  212. */
  213. function _mapStateToProps(state) {
  214. const {
  215. upgradeRoleError,
  216. upgradeRoleInProgress
  217. } = state['features/authentication'];
  218. const { authRequired } = state['features/base/conference'];
  219. const { hosts: configHosts } = state['features/base/config'];
  220. const {
  221. connecting,
  222. error: connectionError,
  223. errorMessage: connectionErrorMessage
  224. } = state['features/base/connection'];
  225. let error;
  226. let errorDetails;
  227. if (connectionError) {
  228. error = connectionError;
  229. errorDetails = connectionErrorMessage;
  230. } else if (upgradeRoleError) {
  231. error
  232. = upgradeRoleError.connectionError
  233. || upgradeRoleError.authenticationError;
  234. errorDetails = upgradeRoleError.message;
  235. }
  236. return {
  237. _conference: authRequired,
  238. _configHosts: configHosts,
  239. _connecting: Boolean(connecting) || Boolean(upgradeRoleInProgress),
  240. _error: error,
  241. _errorDetails: errorDetails
  242. };
  243. }
  244. export default translate(reduxConnect(_mapStateToProps)(LoginDialog));