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

LoginDialog.native.js 8.6KB

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