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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { Text, TextInput, View } from 'react-native';
  4. import { connect as reduxConnect } from 'react-redux';
  5. import type { Dispatch } from 'redux';
  6. import { connect, toJid } from '../../base/connection';
  7. import {
  8. CustomSubmitDialog,
  9. FIELD_UNDERLINE,
  10. PLACEHOLDER_COLOR,
  11. _abstractMapStateToProps,
  12. inputDialog as inputDialogStyle
  13. } from '../../base/dialog';
  14. import { translate } from '../../base/i18n';
  15. import { JitsiConnectionErrors } from '../../base/lib-jitsi-meet';
  16. import { StyleType } from '../../base/styles';
  17. import { authenticateAndUpgradeRole, cancelLogin } from '../actions';
  18. import styles from './styles';
  19. /**
  20. * The type of the React {@link Component} props of {@link LoginDialog}.
  21. */
  22. type Props = {
  23. /**
  24. * {@link JitsiConference} that needs authentication - will hold a valid
  25. * value in XMPP login + guest access mode.
  26. */
  27. _conference: Object,
  28. /**
  29. * The server hosts specified in the global config.
  30. */
  31. _configHosts: Object,
  32. /**
  33. * Indicates if the dialog should display "connecting" status message.
  34. */
  35. _connecting: boolean,
  36. /**
  37. * The color-schemed stylesheet of the base/dialog feature.
  38. */
  39. _dialogStyles: StyleType,
  40. /**
  41. * The error which occurred during login/authentication.
  42. */
  43. _error: Object,
  44. /**
  45. * The progress in the floating range between 0 and 1 of the authenticating
  46. * and upgrading the role of the local participant/user.
  47. */
  48. _progress: number,
  49. /**
  50. * Redux store dispatch method.
  51. */
  52. dispatch: Dispatch<any>,
  53. /**
  54. * Invoked to obtain translated strings.
  55. */
  56. t: Function
  57. };
  58. /**
  59. * The type of the React {@link Component} state of {@link LoginDialog}.
  60. */
  61. type State = {
  62. /**
  63. * The user entered password for the conference.
  64. */
  65. password: string,
  66. /**
  67. * The user entered local participant name.
  68. */
  69. username: string
  70. };
  71. /**
  72. * Dialog asks user for username and password.
  73. *
  74. * First authentication configuration that it will deal with is the main XMPP
  75. * domain (config.hosts.domain) with password authentication. A LoginDialog
  76. * will be opened after 'CONNECTION_FAILED' action with
  77. * 'JitsiConnectionErrors.PASSWORD_REQUIRED' error. After username and password
  78. * are entered a new 'connect' action from 'features/base/connection' will be
  79. * triggered which will result in new XMPP connection. The conference will start
  80. * if the credentials are correct.
  81. *
  82. * The second setup is the main XMPP domain with password plus guest domain with
  83. * anonymous access configured under 'config.hosts.anonymousdomain'. In such
  84. * case user connects from the anonymous domain, but if the room does not exist
  85. * yet, Jicofo will not allow to start new conference. This will trigger
  86. * 'CONFERENCE_FAILED' action with JitsiConferenceErrors.AUTHENTICATION_REQUIRED
  87. * error and 'authRequired' value of 'features/base/conference' will hold
  88. * the {@link JitsiConference} instance. If user decides to authenticate, a
  89. * new/separate XMPP connection is established and authentication is performed.
  90. * In case it succeeds, Jicofo will assign new session ID which then can be used
  91. * from the anonymous domain connection to create and join the room. This part
  92. * is done by {@link JitsiConference#authenticateAndUpgradeRole} in
  93. * lib-jitsi-meet.
  94. *
  95. * See {@link https://github.com/jitsi/jicofo#secure-domain} for a description
  96. * of the configuration parameters.
  97. */
  98. class LoginDialog extends Component<Props, State> {
  99. /**
  100. * Initializes a new LoginDialog instance.
  101. *
  102. * @param {Object} props - The read-only properties with which the new
  103. * instance is to be initialized.
  104. */
  105. constructor(props: Props) {
  106. super(props);
  107. this.state = {
  108. username: '',
  109. password: ''
  110. };
  111. // Bind event handlers so they are only bound once per instance.
  112. this._onCancel = this._onCancel.bind(this);
  113. this._onLogin = this._onLogin.bind(this);
  114. this._onPasswordChange = this._onPasswordChange.bind(this);
  115. this._onUsernameChange = this._onUsernameChange.bind(this);
  116. }
  117. /**
  118. * Implements React's {@link Component#render()}.
  119. *
  120. * @inheritdoc
  121. * @returns {ReactElement}
  122. */
  123. render() {
  124. const {
  125. _connecting: connecting,
  126. _dialogStyles,
  127. _error: error,
  128. _progress: progress,
  129. t
  130. } = this.props;
  131. let messageKey;
  132. const messageOptions = {};
  133. if (progress && progress < 1) {
  134. messageKey = 'connection.FETCH_SESSION_ID';
  135. } else if (error) {
  136. const { name } = error;
  137. if (name === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  138. // Show a message that the credentials are incorrect only if the
  139. // credentials which have caused the connection to fail are the
  140. // ones which the user sees.
  141. const { credentials } = error;
  142. if (credentials
  143. && credentials.jid
  144. === toJid(
  145. this.state.username,
  146. this.props._configHosts)
  147. && credentials.password === this.state.password) {
  148. messageKey = 'dialog.incorrectPassword';
  149. }
  150. } else if (name) {
  151. messageKey = 'dialog.connectErrorWithMsg';
  152. messageOptions.msg = `${name} ${error.message}`;
  153. }
  154. }
  155. const showMessage = messageKey || connecting;
  156. const message = messageKey
  157. ? t(messageKey, messageOptions)
  158. : connecting
  159. ? t('connection.CONNECTING')
  160. : '';
  161. return (
  162. <CustomSubmitDialog
  163. okDisabled = { connecting }
  164. onCancel = { this._onCancel }
  165. onSubmit = { this._onLogin }>
  166. <View style = { styles.loginDialog }>
  167. <TextInput
  168. autoCapitalize = { 'none' }
  169. autoCorrect = { false }
  170. onChangeText = { this._onUsernameChange }
  171. placeholder = { 'user@domain.com' }
  172. placeholderTextColor = { PLACEHOLDER_COLOR }
  173. style = { _dialogStyles.field }
  174. underlineColorAndroid = { FIELD_UNDERLINE }
  175. value = { this.state.username } />
  176. <TextInput
  177. onChangeText = { this._onPasswordChange }
  178. placeholder = { t('dialog.userPassword') }
  179. placeholderTextColor = { PLACEHOLDER_COLOR }
  180. secureTextEntry = { true }
  181. style = { [
  182. _dialogStyles.field,
  183. inputDialogStyle.bottomField
  184. ] }
  185. underlineColorAndroid = { FIELD_UNDERLINE }
  186. value = { this.state.password } />
  187. { showMessage && (
  188. <Text style = { styles.dialogText }>
  189. { message }
  190. </Text>
  191. ) }
  192. </View>
  193. </CustomSubmitDialog>
  194. );
  195. }
  196. _onUsernameChange: (string) => void;
  197. /**
  198. * Called when user edits the username.
  199. *
  200. * @param {string} text - A new username value entered by user.
  201. * @returns {void}
  202. * @private
  203. */
  204. _onUsernameChange(text) {
  205. this.setState({
  206. username: text
  207. });
  208. }
  209. _onPasswordChange: (string) => void;
  210. /**
  211. * Called when user edits the password.
  212. *
  213. * @param {string} text - A new password value entered by user.
  214. * @returns {void}
  215. * @private
  216. */
  217. _onPasswordChange(text) {
  218. this.setState({
  219. password: text
  220. });
  221. }
  222. _onCancel: () => void;
  223. /**
  224. * Notifies this LoginDialog that it has been dismissed by cancel.
  225. *
  226. * @private
  227. * @returns {void}
  228. */
  229. _onCancel() {
  230. this.props.dispatch(cancelLogin());
  231. }
  232. _onLogin: () => void;
  233. /**
  234. * Notifies this LoginDialog that the login button (OK) has been pressed by
  235. * the user.
  236. *
  237. * @private
  238. * @returns {void}
  239. */
  240. _onLogin() {
  241. const { _conference: conference, dispatch } = this.props;
  242. const { password, username } = this.state;
  243. const jid = toJid(username, this.props._configHosts);
  244. let r;
  245. // If there's a conference it means that the connection has succeeded,
  246. // but authentication is required in order to join the room.
  247. if (conference) {
  248. r = dispatch(authenticateAndUpgradeRole(jid, password, conference));
  249. } else {
  250. r = dispatch(connect(jid, password));
  251. }
  252. return r;
  253. }
  254. }
  255. /**
  256. * Maps (parts of) the Redux state to the associated props for the
  257. * {@code LoginDialog} component.
  258. *
  259. * @param {Object} state - The Redux state.
  260. * @private
  261. * @returns {{
  262. * _conference: JitsiConference,
  263. * _configHosts: Object,
  264. * _connecting: boolean,
  265. * _dialogStyles: StyleType,
  266. * _error: Object,
  267. * _progress: number
  268. * }}
  269. */
  270. function _mapStateToProps(state) {
  271. const {
  272. error: authenticateAndUpgradeRoleError,
  273. progress,
  274. thenableWithCancel
  275. } = state['features/authentication'];
  276. const { authRequired } = state['features/base/conference'];
  277. const { hosts: configHosts } = state['features/base/config'];
  278. const {
  279. connecting,
  280. error: connectionError
  281. } = state['features/base/connection'];
  282. return {
  283. ..._abstractMapStateToProps(state),
  284. _conference: authRequired,
  285. _configHosts: configHosts,
  286. _connecting: Boolean(connecting) || Boolean(thenableWithCancel),
  287. _error: connectionError || authenticateAndUpgradeRoleError,
  288. _progress: progress
  289. };
  290. }
  291. export default translate(reduxConnect(_mapStateToProps)(LoginDialog));