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

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