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.tsx 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 { toJid } from '../../../base/connection/functions';
  8. import { translate, translateToHTML } from '../../../base/i18n/functions';
  9. import { JitsiConnectionErrors } from '../../../base/lib-jitsi-meet';
  10. import Dialog from '../../../base/ui/components/web/Dialog';
  11. import Input from '../../../base/ui/components/web/Input';
  12. import { joinConference } from '../../../prejoin/actions.web';
  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. // FIXME: Workaround for the web version. To be removed once we get rid of conference.js
  120. dispatch(joinConference(undefined, false, jid, password));
  121. }
  122. }
  123. /**
  124. * Callback for the onChange event of the field.
  125. *
  126. * @param {string} value - The static event.
  127. * @returns {void}
  128. */
  129. _onPasswordChange(value: string) {
  130. this.setState({
  131. password: value
  132. });
  133. }
  134. /**
  135. * Callback for the onChange event of the username input.
  136. *
  137. * @param {string} value - The new value.
  138. * @returns {void}
  139. */
  140. _onUsernameChange(value: string) {
  141. this.setState({
  142. username: value
  143. });
  144. }
  145. /**
  146. * Renders an optional message, if applicable.
  147. *
  148. * @returns {ReactElement}
  149. * @private
  150. */
  151. renderMessage() {
  152. const {
  153. _configHosts: configHosts,
  154. _connecting: connecting,
  155. _error: error,
  156. _progress: progress,
  157. t
  158. } = this.props;
  159. const { username, password } = this.state;
  160. const messageOptions: { msg?: string; } = {};
  161. let messageKey;
  162. if (progress && progress < 1) {
  163. messageKey = 'connection.FETCH_SESSION_ID';
  164. } else if (error) {
  165. const { name } = error;
  166. if (name === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  167. const { credentials } = error;
  168. if (credentials
  169. && credentials.jid === toJid(username, configHosts ?? { authdomain: '',
  170. domain: '' })
  171. && credentials.password === password) {
  172. messageKey = 'dialog.incorrectPassword';
  173. }
  174. } else if (name) {
  175. messageKey = 'dialog.connectErrorWithMsg';
  176. messageOptions.msg = `${name} ${error.message}`;
  177. }
  178. } else if (connecting) {
  179. messageKey = 'connection.CONNECTING';
  180. }
  181. if (messageKey) {
  182. return (
  183. <span>
  184. { translateToHTML(t, messageKey, messageOptions) }
  185. </span>
  186. );
  187. }
  188. return null;
  189. }
  190. /**
  191. * Implements {@Component#render}.
  192. *
  193. * @inheritdoc
  194. */
  195. render() {
  196. const {
  197. _connecting: connecting,
  198. t
  199. } = this.props;
  200. const { password, username } = this.state;
  201. return (
  202. <Dialog
  203. disableAutoHideOnSubmit = { true }
  204. disableBackdropClose = { true }
  205. hideCloseButton = { true }
  206. ok = {{
  207. disabled: connecting
  208. || !password
  209. || !username,
  210. translationKey: 'dialog.login'
  211. }}
  212. onCancel = { this._onCancelLogin }
  213. onSubmit = { this._onLogin }
  214. titleKey = { t('dialog.authenticationRequired') }>
  215. <Input
  216. autoFocus = { true }
  217. id = 'login-dialog-username'
  218. label = { t('dialog.user') }
  219. name = 'username'
  220. onChange = { this._onUsernameChange }
  221. placeholder = { t('dialog.userIdentifier') }
  222. type = 'text'
  223. value = { username } />
  224. <br />
  225. <Input
  226. className = 'dialog-bottom-margin'
  227. id = 'login-dialog-password'
  228. label = { t('dialog.userPassword') }
  229. name = 'password'
  230. onChange = { this._onPasswordChange }
  231. placeholder = { t('dialog.password') }
  232. type = 'password'
  233. value = { password } />
  234. { this.renderMessage() }
  235. </Dialog>
  236. );
  237. }
  238. }
  239. /**
  240. * Maps (parts of) the Redux state to the associated props for the
  241. * {@code LoginDialog} component.
  242. *
  243. * @param {Object} state - The Redux state.
  244. * @private
  245. * @returns {IProps}
  246. */
  247. function mapStateToProps(state: IReduxState) {
  248. const {
  249. error: authenticateAndUpgradeRoleError,
  250. progress,
  251. thenableWithCancel
  252. } = state['features/authentication'];
  253. const { authRequired, conference } = state['features/base/conference'];
  254. const { hosts: configHosts } = state['features/base/config'];
  255. const {
  256. connecting,
  257. error: connectionError
  258. } = state['features/base/connection'];
  259. return {
  260. _conference: authRequired || conference,
  261. _configHosts: configHosts,
  262. _connecting: Boolean(connecting) || Boolean(thenableWithCancel),
  263. _error: connectionError || authenticateAndUpgradeRoleError,
  264. _progress: progress
  265. };
  266. }
  267. export default translate(reduxConnect(mapStateToProps)(LoginDialog));