LoginCtrl.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React from 'react';
  2. import config from 'app/core/config';
  3. import { updateLocation } from 'app/core/actions';
  4. import { connect } from 'react-redux';
  5. import { StoreState } from 'app/types';
  6. import { PureComponent } from 'react';
  7. import { getBackendSrv } from '@grafana/runtime';
  8. import { hot } from 'react-hot-loader';
  9. import appEvents from 'app/core/app_events';
  10. const isOauthEnabled = () => Object.keys(config.oauth).length > 0;
  11. export interface FormModel {
  12. user: string;
  13. password: string;
  14. email: string;
  15. }
  16. interface Props {
  17. routeParams?: any;
  18. updateLocation?: typeof updateLocation;
  19. children: (props: {
  20. isLoggingIn: boolean;
  21. changePassword: (pw: string) => void;
  22. isChangingPassword: boolean;
  23. skipPasswordChange: Function;
  24. login: (data: FormModel) => void;
  25. disableLoginForm: boolean;
  26. ldapEnabled: boolean;
  27. authProxyEnabled: boolean;
  28. disableUserSignUp: boolean;
  29. isOauthEnabled: boolean;
  30. loginHint: string;
  31. passwordHint: string;
  32. }) => JSX.Element;
  33. }
  34. interface State {
  35. isLoggingIn: boolean;
  36. isChangingPassword: boolean;
  37. }
  38. export class LoginCtrl extends PureComponent<Props, State> {
  39. result: any = {};
  40. constructor(props: Props) {
  41. super(props);
  42. this.state = {
  43. isLoggingIn: false,
  44. isChangingPassword: false,
  45. };
  46. if (config.loginError) {
  47. appEvents.emit('alert-warning', ['Login Failed', config.loginError]);
  48. }
  49. }
  50. changePassword = (password: string) => {
  51. const pw = {
  52. newPassword: password,
  53. confirmNew: password,
  54. oldPassword: 'admin',
  55. };
  56. getBackendSrv()
  57. .put('/api/user/password', pw)
  58. .then(() => {
  59. this.toGrafana();
  60. })
  61. .catch((err: any) => console.log(err));
  62. };
  63. login = (formModel: FormModel) => {
  64. this.setState({
  65. isLoggingIn: true,
  66. });
  67. getBackendSrv()
  68. .post('/login', formModel)
  69. .then((result: any) => {
  70. this.result = result;
  71. if (formModel.password !== 'admin' || config.ldapEnabled || config.authProxyEnabled) {
  72. this.toGrafana();
  73. return;
  74. } else {
  75. this.changeView();
  76. }
  77. })
  78. .catch(() => {
  79. this.setState({
  80. isLoggingIn: false,
  81. });
  82. });
  83. };
  84. changeView = () => {
  85. this.setState({
  86. isChangingPassword: true,
  87. });
  88. };
  89. toGrafana = () => {
  90. const params = this.props.routeParams;
  91. // Use window.location.href to force page reload
  92. if (params.redirect && params.redirect[0] === '/') {
  93. window.location.href = config.appSubUrl + params.redirect;
  94. } else if (this.result.redirectUrl) {
  95. window.location.href = config.appSubUrl + this.result.redirectUrl;
  96. } else {
  97. window.location.href = config.appSubUrl + '/';
  98. }
  99. };
  100. render() {
  101. const { children } = this.props;
  102. const { isLoggingIn, isChangingPassword } = this.state;
  103. const { login, toGrafana, changePassword } = this;
  104. const { loginHint, passwordHint, disableLoginForm, ldapEnabled, authProxyEnabled, disableUserSignUp } = config;
  105. return (
  106. <>
  107. {children({
  108. isOauthEnabled: isOauthEnabled(),
  109. loginHint,
  110. passwordHint,
  111. disableLoginForm,
  112. ldapEnabled,
  113. authProxyEnabled,
  114. disableUserSignUp,
  115. login,
  116. isLoggingIn,
  117. changePassword,
  118. skipPasswordChange: toGrafana,
  119. isChangingPassword,
  120. })}
  121. </>
  122. );
  123. }
  124. }
  125. export const mapStateToProps = (state: StoreState) => ({
  126. routeParams: state.location.routeParams,
  127. });
  128. const mapDispatchToProps = { updateLocation };
  129. export default hot(module)(
  130. connect(
  131. mapStateToProps,
  132. mapDispatchToProps
  133. )(LoginCtrl)
  134. );