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.

LoginContainer.tsx 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { Location } from 'history';
  21. import * as React from 'react';
  22. import { connect } from 'react-redux';
  23. import { getReturnUrl } from 'sonar-ui-common/helpers/urls';
  24. import { getIdentityProviders } from '../../../api/users';
  25. import { doLogin } from '../../../store/rootActions';
  26. import Login from './Login';
  27. interface OwnProps {
  28. location: Pick<Location, 'hash' | 'pathname' | 'query'> & {
  29. query: { advanced?: string; return_to?: string };
  30. };
  31. }
  32. interface DispatchToProps {
  33. doLogin: (login: string, password: string) => Promise<void>;
  34. }
  35. type Props = OwnProps & DispatchToProps;
  36. interface State {
  37. identityProviders?: T.IdentityProvider[];
  38. }
  39. export class LoginContainer extends React.PureComponent<Props, State> {
  40. mounted = false;
  41. state: State = {};
  42. componentDidMount() {
  43. this.mounted = true;
  44. getIdentityProviders().then(
  45. ({ identityProviders }) => {
  46. if (this.mounted) {
  47. this.setState({ identityProviders });
  48. }
  49. },
  50. () => {}
  51. );
  52. }
  53. componentWillUnmount() {
  54. this.mounted = false;
  55. }
  56. handleSuccessfulLogin = () => {
  57. window.location.href = getReturnUrl(this.props.location);
  58. };
  59. handleSubmit = (login: string, password: string) => {
  60. return this.props.doLogin(login, password).then(this.handleSuccessfulLogin, () => {});
  61. };
  62. render() {
  63. const { location } = this.props;
  64. const { identityProviders } = this.state;
  65. if (!identityProviders) {
  66. return null;
  67. }
  68. return (
  69. <Login
  70. identityProviders={identityProviders}
  71. onSubmit={this.handleSubmit}
  72. returnTo={getReturnUrl(location)}
  73. />
  74. );
  75. }
  76. }
  77. const mapStateToProps = null;
  78. const mapDispatchToProps = { doLogin: doLogin as any };
  79. export default connect(mapStateToProps, mapDispatchToProps)(LoginContainer);