您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LoginDialog.native.js 10KB

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