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

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