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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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';
  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. style = { _dialogStyles.field }
  150. underlineColorAndroid = { FIELD_UNDERLINE }
  151. value = { this.state.username } />
  152. <TextInput
  153. onChangeText = { this._onPasswordChange }
  154. placeholder = { t('dialog.userPassword') }
  155. placeholderTextColor = { PLACEHOLDER_COLOR }
  156. secureTextEntry = { true }
  157. style = { [
  158. _dialogStyles.field,
  159. inputDialogStyle.bottomField
  160. ] }
  161. underlineColorAndroid = { FIELD_UNDERLINE }
  162. value = { this.state.password } />
  163. { this._renderMessage() }
  164. </View>
  165. </CustomSubmitDialog>
  166. );
  167. }
  168. /**
  169. * Renders an optional message, if applicable.
  170. *
  171. * @returns {ReactElement}
  172. * @private
  173. */
  174. _renderMessage() {
  175. const {
  176. _connecting: connecting,
  177. _error: error,
  178. _progress: progress,
  179. _styles: styles,
  180. t
  181. } = this.props;
  182. let messageKey;
  183. let messageIsError = false;
  184. const messageOptions = {};
  185. if (progress && progress < 1) {
  186. messageKey = 'connection.FETCH_SESSION_ID';
  187. } else if (error) {
  188. const { name } = error;
  189. if (name === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  190. // Show a message that the credentials are incorrect only if the
  191. // credentials which have caused the connection to fail are the
  192. // ones which the user sees.
  193. const { credentials } = error;
  194. if (credentials
  195. && credentials.jid
  196. === toJid(
  197. this.state.username,
  198. this.props._configHosts)
  199. && credentials.password === this.state.password) {
  200. messageKey = 'dialog.incorrectPassword';
  201. messageIsError = true;
  202. }
  203. } else if (name) {
  204. messageKey = 'dialog.connectErrorWithMsg';
  205. messageOptions.msg = `${name} ${error.message}`;
  206. messageIsError = true;
  207. }
  208. } else if (connecting) {
  209. messageKey = 'connection.CONNECTING';
  210. }
  211. if (messageKey) {
  212. const message = t(messageKey, messageOptions);
  213. const messageStyles = [
  214. styles.dialogText,
  215. messageIsError ? styles.errorMessage : styles.progressMessage
  216. ];
  217. return (
  218. <Text style = { messageStyles }>
  219. { message }
  220. </Text>
  221. );
  222. }
  223. return null;
  224. }
  225. _onUsernameChange: (string) => void;
  226. /**
  227. * Called when user edits the username.
  228. *
  229. * @param {string} text - A new username value entered by user.
  230. * @returns {void}
  231. * @private
  232. */
  233. _onUsernameChange(text) {
  234. this.setState({
  235. username: text
  236. });
  237. }
  238. _onPasswordChange: (string) => void;
  239. /**
  240. * Called when user edits the password.
  241. *
  242. * @param {string} text - A new password value entered by user.
  243. * @returns {void}
  244. * @private
  245. */
  246. _onPasswordChange(text) {
  247. this.setState({
  248. password: text
  249. });
  250. }
  251. _onCancel: () => void;
  252. /**
  253. * Notifies this LoginDialog that it has been dismissed by cancel.
  254. *
  255. * @private
  256. * @returns {void}
  257. */
  258. _onCancel() {
  259. this.props.dispatch(cancelLogin());
  260. }
  261. _onLogin: () => void;
  262. /**
  263. * Notifies this LoginDialog that the login button (OK) has been pressed by
  264. * the user.
  265. *
  266. * @private
  267. * @returns {void}
  268. */
  269. _onLogin() {
  270. const { _conference: conference, dispatch } = this.props;
  271. const { password, username } = this.state;
  272. const jid = toJid(username, this.props._configHosts);
  273. let r;
  274. // If there's a conference it means that the connection has succeeded,
  275. // but authentication is required in order to join the room.
  276. if (conference) {
  277. r = dispatch(authenticateAndUpgradeRole(jid, password, conference));
  278. } else {
  279. r = dispatch(connect(jid, password));
  280. }
  281. return r;
  282. }
  283. }
  284. /**
  285. * Maps (parts of) the Redux state to the associated props for the
  286. * {@code LoginDialog} component.
  287. *
  288. * @param {Object} state - The Redux state.
  289. * @private
  290. * @returns {Props}
  291. */
  292. function _mapStateToProps(state) {
  293. const {
  294. error: authenticateAndUpgradeRoleError,
  295. progress,
  296. thenableWithCancel
  297. } = state['features/authentication'];
  298. const { authRequired } = state['features/base/conference'];
  299. const { hosts: configHosts } = state['features/base/config'];
  300. const {
  301. connecting,
  302. error: connectionError
  303. } = state['features/base/connection'];
  304. return {
  305. ..._abstractMapStateToProps(state),
  306. _conference: authRequired,
  307. _configHosts: configHosts,
  308. _connecting: Boolean(connecting) || Boolean(thenableWithCancel),
  309. _error: connectionError || authenticateAndUpgradeRoleError,
  310. _progress: progress,
  311. _styles: ColorSchemeRegistry.get(state, 'LoginDialog')
  312. };
  313. }
  314. export default translate(reduxConnect(_mapStateToProps)(LoginDialog));