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.js 11KB

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