You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginDialog.native.js 9.0KB

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