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

LoginDialog.native.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * The credential that the user has failed to authenticate with.
  64. */
  65. _errorCredentials: PropTypes.object,
  66. /**
  67. * Any extra details about the error provided by lib-jitsi-meet.
  68. */
  69. _errorDetails: PropTypes.string,
  70. /**
  71. * Redux store dispatch method.
  72. */
  73. dispatch: PropTypes.func,
  74. /**
  75. * Invoked to obtain translated strings.
  76. */
  77. t: PropTypes.func
  78. };
  79. /**
  80. * Initializes a new LoginDialog instance.
  81. *
  82. * @param {Object} props - The read-only properties with which the new
  83. * instance is to be initialized.
  84. */
  85. constructor(props) {
  86. super(props);
  87. this.state = {
  88. username: '',
  89. password: ''
  90. };
  91. // Bind event handlers so they are only bound once per instance.
  92. this._onCancel = this._onCancel.bind(this);
  93. this._onLogin = this._onLogin.bind(this);
  94. this._onPasswordChange = this._onPasswordChange.bind(this);
  95. this._onUsernameChange = this._onUsernameChange.bind(this);
  96. }
  97. /**
  98. * Implements React's {@link Component#render()}.
  99. *
  100. * @inheritdoc
  101. * @returns {ReactElement}
  102. */
  103. render() {
  104. const {
  105. _connecting: connecting,
  106. _error: error,
  107. _errorCredentials: errorCredentials,
  108. _errorDetails: errorDetails,
  109. t
  110. } = this.props;
  111. let messageKey = '';
  112. const messageOptions = {};
  113. if (error === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  114. // Show the message if there's been a user ID or password provided.
  115. messageKey
  116. = errorCredentials
  117. && (errorCredentials.jid || errorCredentials.password)
  118. ? 'dialog.incorrectPassword'
  119. : null;
  120. } else if (error) {
  121. messageKey = 'dialog.connectErrorWithMsg';
  122. messageOptions.msg = `${error} ${errorDetails}`;
  123. }
  124. return (
  125. <Dialog
  126. okDisabled = { connecting }
  127. onCancel = { this._onCancel }
  128. onSubmit = { this._onLogin }
  129. titleKey = 'dialog.passwordRequired'>
  130. <View style = { styles.loginDialog }>
  131. <TextInput
  132. onChangeText = { this._onUsernameChange }
  133. placeholder = { 'user@domain.com' }
  134. style = { styles.loginDialogTextInput }
  135. value = { this.state.username } />
  136. <TextInput
  137. onChangeText = { this._onPasswordChange }
  138. placeholder = { t('dialog.userPassword') }
  139. secureTextEntry = { true }
  140. style = { styles.loginDialogTextInput }
  141. value = { this.state.password } />
  142. <Text style = { styles.loginDialogText }>
  143. {
  144. messageKey
  145. ? t(messageKey, messageOptions)
  146. : connecting
  147. ? t('connection.CONNECTING')
  148. : ''
  149. }
  150. </Text>
  151. </View>
  152. </Dialog>
  153. );
  154. }
  155. /**
  156. * Called when user edits the username.
  157. *
  158. * @param {string} text - A new username value entered by user.
  159. * @returns {void}
  160. * @private
  161. */
  162. _onUsernameChange(text) {
  163. this.setState({
  164. username: text
  165. });
  166. }
  167. /**
  168. * Called when user edits the password.
  169. *
  170. * @param {string} text - A new password value entered by user.
  171. * @returns {void}
  172. * @private
  173. */
  174. _onPasswordChange(text) {
  175. this.setState({
  176. password: text
  177. });
  178. }
  179. /**
  180. * Notifies this LoginDialog that it has been dismissed by cancel.
  181. *
  182. * @private
  183. * @returns {void}
  184. */
  185. _onCancel() {
  186. this.props.dispatch(cancelLogin());
  187. }
  188. /**
  189. * Notifies this LoginDialog that the login button (OK) has been pressed by
  190. * the user.
  191. *
  192. * @private
  193. * @returns {void}
  194. */
  195. _onLogin() {
  196. const { _conference: conference } = this.props;
  197. const { username, password } = this.state;
  198. const jid = toJid(username, this.props._configHosts);
  199. // If there's a conference it means that the connection has succeeded,
  200. // but authentication is required in order to join the room.
  201. if (conference) {
  202. this.props.dispatch(
  203. authenticateAndUpgradeRole(jid, password, conference));
  204. } else {
  205. this.props.dispatch(connect(jid, password));
  206. }
  207. }
  208. }
  209. /**
  210. * Maps (parts of) the Redux state to the associated props for the
  211. * {@code LoginDialog} component.
  212. *
  213. * @param {Object} state - The Redux state.
  214. * @private
  215. * @returns {{
  216. * _conference: JitsiConference,
  217. * _configHosts: Object,
  218. * _connecting: boolean,
  219. * _error: string,
  220. * _errorCredentials: Object,
  221. * _errorDetails: string
  222. * }}
  223. */
  224. function _mapStateToProps(state) {
  225. const {
  226. upgradeRoleError,
  227. upgradeRoleInProgress
  228. } = state['features/authentication'];
  229. const { authRequired } = state['features/base/conference'];
  230. const { hosts: configHosts } = state['features/base/config'];
  231. const {
  232. connecting,
  233. credentials,
  234. error: connectionError,
  235. errorMessage: connectionErrorMessage
  236. } = state['features/base/connection'];
  237. let error;
  238. let errorCredentials;
  239. let errorDetails;
  240. if (connectionError) {
  241. error = connectionError;
  242. errorCredentials = credentials;
  243. errorDetails = connectionErrorMessage;
  244. } else if (upgradeRoleError) {
  245. error
  246. = upgradeRoleError.connectionError
  247. || upgradeRoleError.authenticationError;
  248. errorCredentials = upgradeRoleError.credentials;
  249. errorDetails = upgradeRoleError.message;
  250. }
  251. return {
  252. _conference: authRequired,
  253. _configHosts: configHosts,
  254. _connecting: Boolean(connecting) || Boolean(upgradeRoleInProgress),
  255. _error: error,
  256. _errorCredentials: errorCredentials,
  257. _errorDetails: errorDetails
  258. };
  259. }
  260. export default translate(reduxConnect(_mapStateToProps)(LoginDialog));