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

LoginDialog.tsx 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React, { Component } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { connect as reduxConnect } from 'react-redux';
  4. import { IReduxState, IStore } from '../../../app/types';
  5. import { IJitsiConference } from '../../../base/conference/reducer';
  6. import { IConfig } from '../../../base/config/configType';
  7. import { connect } from '../../../base/connection/actions.web';
  8. import { toJid } from '../../../base/connection/functions';
  9. import { translate, translateToHTML } from '../../../base/i18n/functions';
  10. import { JitsiConnectionErrors } from '../../../base/lib-jitsi-meet';
  11. import Dialog from '../../../base/ui/components/web/Dialog';
  12. import Input from '../../../base/ui/components/web/Input';
  13. import {
  14. authenticateAndUpgradeRole,
  15. cancelLogin
  16. } from '../../actions.web';
  17. /**
  18. * The type of the React {@code Component} props of {@link LoginDialog}.
  19. */
  20. interface IProps extends WithTranslation {
  21. /**
  22. * {@link JitsiConference} That needs authentication - will hold a valid
  23. * value in XMPP login + guest access mode.
  24. */
  25. _conference?: IJitsiConference;
  26. /**
  27. * The server hosts specified in the global config.
  28. */
  29. _configHosts: IConfig['hosts'];
  30. /**
  31. * Indicates if the dialog should display "connecting" status message.
  32. */
  33. _connecting: boolean;
  34. /**
  35. * The error which occurred during login/authentication.
  36. */
  37. _error: any;
  38. /**
  39. * The progress in the floating range between 0 and 1 of the authenticating
  40. * and upgrading the role of the local participant/user.
  41. */
  42. _progress?: number;
  43. /**
  44. * Redux store dispatch method.
  45. */
  46. dispatch: IStore['dispatch'];
  47. /**
  48. * Conference room name.
  49. */
  50. roomName: string;
  51. }
  52. /**
  53. * The type of the React {@code Component} state of {@link LoginDialog}.
  54. */
  55. interface IState {
  56. /**
  57. * The user entered password for the conference.
  58. */
  59. password: string;
  60. /**
  61. * The user entered local participant name.
  62. */
  63. username: string;
  64. }
  65. /**
  66. * Component that renders the login in conference dialog.
  67. *
  68. * @returns {React$Element<any>}
  69. */
  70. class LoginDialog extends Component<IProps, IState> {
  71. /**
  72. * Initializes a new {@code LoginDialog} instance.
  73. *
  74. * @inheritdoc
  75. */
  76. constructor(props: IProps) {
  77. super(props);
  78. this.state = {
  79. username: '',
  80. password: ''
  81. };
  82. this._onCancelLogin = this._onCancelLogin.bind(this);
  83. this._onLogin = this._onLogin.bind(this);
  84. this._onUsernameChange = this._onUsernameChange.bind(this);
  85. this._onPasswordChange = this._onPasswordChange.bind(this);
  86. }
  87. /**
  88. * Called when the cancel button is clicked.
  89. *
  90. * @private
  91. * @returns {void}
  92. */
  93. _onCancelLogin() {
  94. const { dispatch } = this.props;
  95. dispatch(cancelLogin());
  96. }
  97. /**
  98. * Notifies this LoginDialog that the login button (OK) has been pressed by
  99. * the user.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _onLogin() {
  105. const {
  106. _conference: conference,
  107. _configHosts: configHosts,
  108. dispatch
  109. } = this.props;
  110. const { password, username } = this.state;
  111. const jid = toJid(username, configHosts ?? {
  112. authdomain: '',
  113. domain: ''
  114. });
  115. if (conference) {
  116. dispatch(authenticateAndUpgradeRole(jid, password, conference));
  117. } else {
  118. dispatch(connect(jid, password));
  119. }
  120. }
  121. /**
  122. * Callback for the onChange event of the field.
  123. *
  124. * @param {string} value - The static event.
  125. * @returns {void}
  126. */
  127. _onPasswordChange(value: string) {
  128. this.setState({
  129. password: value
  130. });
  131. }
  132. /**
  133. * Callback for the onChange event of the username input.
  134. *
  135. * @param {string} value - The new value.
  136. * @returns {void}
  137. */
  138. _onUsernameChange(value: string) {
  139. this.setState({
  140. username: value
  141. });
  142. }
  143. /**
  144. * Renders an optional message, if applicable.
  145. *
  146. * @returns {ReactElement}
  147. * @private
  148. */
  149. renderMessage() {
  150. const {
  151. _configHosts: configHosts,
  152. _connecting: connecting,
  153. _error: error,
  154. _progress: progress,
  155. t
  156. } = this.props;
  157. const { username, password } = this.state;
  158. const messageOptions: { msg?: string; } = {};
  159. let messageKey;
  160. if (progress && progress < 1) {
  161. messageKey = 'connection.FETCH_SESSION_ID';
  162. } else if (error) {
  163. const { name } = error;
  164. if (name === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  165. const { credentials } = error;
  166. if (credentials
  167. && credentials.jid === toJid(username, configHosts ?? { authdomain: '',
  168. domain: '' })
  169. && credentials.password === password) {
  170. messageKey = 'dialog.incorrectPassword';
  171. }
  172. } else if (name) {
  173. messageKey = 'dialog.connectErrorWithMsg';
  174. messageOptions.msg = `${name} ${error.message}`;
  175. }
  176. } else if (connecting) {
  177. messageKey = 'connection.CONNECTING';
  178. }
  179. if (messageKey) {
  180. return (
  181. <span>
  182. { translateToHTML(t, messageKey, messageOptions) }
  183. </span>
  184. );
  185. }
  186. return null;
  187. }
  188. /**
  189. * Implements {@Component#render}.
  190. *
  191. * @inheritdoc
  192. */
  193. render() {
  194. const {
  195. _connecting: connecting,
  196. t
  197. } = this.props;
  198. const { password, username } = this.state;
  199. return (
  200. <Dialog
  201. disableAutoHideOnSubmit = { true }
  202. disableBackdropClose = { true }
  203. hideCloseButton = { true }
  204. ok = {{
  205. disabled: connecting
  206. || !password
  207. || !username,
  208. translationKey: 'dialog.login'
  209. }}
  210. onCancel = { this._onCancelLogin }
  211. onSubmit = { this._onLogin }
  212. titleKey = { t('dialog.authenticationRequired') }>
  213. <Input
  214. autoFocus = { true }
  215. id = 'login-dialog-username'
  216. label = { t('dialog.user') }
  217. name = 'username'
  218. onChange = { this._onUsernameChange }
  219. placeholder = { t('dialog.userIdentifier') }
  220. type = 'text'
  221. value = { username } />
  222. <br />
  223. <Input
  224. className = 'dialog-bottom-margin'
  225. id = 'login-dialog-password'
  226. label = { t('dialog.userPassword') }
  227. name = 'password'
  228. onChange = { this._onPasswordChange }
  229. placeholder = { t('dialog.password') }
  230. type = 'password'
  231. value = { password } />
  232. { this.renderMessage() }
  233. </Dialog>
  234. );
  235. }
  236. }
  237. /**
  238. * Maps (parts of) the Redux state to the associated props for the
  239. * {@code LoginDialog} component.
  240. *
  241. * @param {Object} state - The Redux state.
  242. * @private
  243. * @returns {IProps}
  244. */
  245. function mapStateToProps(state: IReduxState) {
  246. const {
  247. error: authenticateAndUpgradeRoleError,
  248. progress,
  249. thenableWithCancel
  250. } = state['features/authentication'];
  251. const { authRequired, conference } = state['features/base/conference'];
  252. const { hosts: configHosts } = state['features/base/config'];
  253. const {
  254. connecting,
  255. error: connectionError
  256. } = state['features/base/connection'];
  257. return {
  258. _conference: authRequired || conference,
  259. _configHosts: configHosts,
  260. _connecting: Boolean(connecting) || Boolean(thenableWithCancel),
  261. _error: connectionError || authenticateAndUpgradeRoleError,
  262. _progress: progress
  263. };
  264. }
  265. export default translate(reduxConnect(mapStateToProps)(LoginDialog));