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

LoginDialog.native.js 9.7KB

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