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.1KB

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